Click here to Skip to main content
15,881,725 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I'm new in java and want to learn it.I have a problem what i cant solve:

Sum of the reversed first x numbers.

I can reverse a number but i dont get it how to do it with multiple for exmp with a for loop. Can any1 help?Or explain it? im newb sry

My code so far:

Java
int input;
	static int revers = 0;
	static int sum;
	static int i;
	
	public static void main (String [] args){
		
		System.out.println("Enter the max number :");
		int input = new Scanner(System.in).nextInt();
		
		 for(int i = 1;i <= input; i++){
			
			
			
			
	        System.out.print(" "+ i);
		 }
////////////**	 I dont get this part where i should make the reverse for the i numbers till input...


		 i = i * 10;
         i = i + input%10;
         input = input/10;
         sum = i + input;
		
*/////////////

		  System.out.println("\n Reverse of input number is: "+ revers);
		  System.out.println("\n And the sum : "+ sum);
Posted
Updated 14-Mar-15 3:31am
v2

1 solution

This smells a lot like homework, so no code! But it's pretty simple.

Start by writing a function to "reverse a number" - you pass it 1234 and it returns 4321.
You can then test that in isolation to make sure it works in all cases (and it's pretty simple to write: a loop with a modulous 10 to extract a digit. Then multiply a current total by ten and add the digit. Divide the number you started with by ten and you're away round the loop until the input number is zero).

Then all you need is to loop from 1 to x adding the reversed values of x.
Print the total, and you're done!
 
Share this answer
 
Comments
Member 11524654 14-Mar-15 9:53am    
I dont need a code i can do it myself, i just need some1 to explain me how to.
Member 11524654 14-Mar-15 10:17am    
K i think i managed to make it. I simple made a nother for loop which reverses the numbers and made a sum for it.Any opinions? int input;
static int revers = 0;
static int sum;
static int i;
static int j;

public static void main (String [] args){

System.out.println("Enter the max number :");
int input = new Scanner(System.in).nextInt();

for(int i = 1;i <= input; i++){
System.out.print(" "+ i+ " ");

}
for (int j = input;j >= i;j--){
System.out.print(" "+ j);
sum +=j;
}



sum +=j;

System.out.println("\n And the sum : "+ sum);
OriginalGriff 14-Mar-15 10:59am    
Um.
Does it fulfil the requirements?
I'd say: probably not. If I enter "12" what value should it output? Does it output that?
I'd say the the "sum of the reversed first 12 numbers" is
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 1 + 11 + 21 + 31 = 109
not
13 + 12 + 11 + 10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 = 91
simply because
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 = 91
as well, so the "reversing" is irrelevant!

Try it!
Member 11524654 14-Mar-15 11:45am    
oh i get it...Ty
OriginalGriff 14-Mar-15 11:47am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900