Hi, I need assistance aka HELP! I've done this calculation in various ways some of printed nothing but zeros non stop. Using while loop calculate all numbers 1 to 1000 add numbers to sum = sum + i int sum = 0; // use a while loop to calculate the sum of 1 to 1000 While(int sum = 0; sum >= 1000; sum = sum + I){ System.out.println(sum); } | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Hi, I need assistance aka HELP! I've done this calculation in various ways some of printed nothing but zeros non stop. Using while loop calculate all numbers 1 to 1000 add numbers to sum = sum + i int sum = 0; // use a while loop to calculate the sum of 1 to 1000 While(int sum = 0; sum >= 1000; sum = sum + I){ System.out.println(sum); }

while Loops

9th Oct 2016, 6:59 PM
Sherelle Rippy
Sherelle Rippy - avatar
4 Answers
+ 2
I wouldn't recommend nesting a for loop inside it like that. int sum = 0; int count = 1; while (count <= 1000) { sum += count; count++; } System.out.println(sum);
9th Oct 2016, 11:48 PM
Liam
Liam - avatar
+ 1
sum = 0; while(sum <= 1000){ for(int I = 1; I <= 1000; I++){ sum += I; } } System.out.print(sum);
9th Oct 2016, 7:12 PM
Wanderlei Borges
Wanderlei Borges - avatar
+ 1
Use the code of Liam, is the correct.
10th Oct 2016, 12:13 AM
Wanderlei Borges
Wanderlei Borges - avatar
0
thanks
9th Oct 2016, 8:30 PM
Sherelle Rippy
Sherelle Rippy - avatar