0
How can a get a sum of a loop?
Need the output to look exactly like this: Enter the upper limit: 500 1 + 2 + 4 + 8 + 16 + 32 + 64 + 128 + 256 = 511 This is what I have so far and it does not work, what should I do: int limit, i = 1; long sum = 1; //Input System.out.print("Enter the upper limit:"); limit = input.nextInt(); //Setting up the math for(int I; i <= limit; i*=2) { System.out.print(i+"+" = sum);
2 Respuestas
+ 1
First of all:
welcome to Sololearn 😊
And nice question style, this is how it should be done 👍
I'm no expert in java, so I can't say much about the syntax.
But conceptionally you are on the right track :)
Some hints:
- initialize sum to 0, not to 1
- add i to sum inside the loop
- exclude to last step from the loop, to print the last number, = and the final sum
0
Thank you Matthias! Can you go into more details in that