Use a while loop to calculate the sum of all numbers from 1 to 1000. Make it print only the sum, not the intermediate values. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Use a while loop to calculate the sum of all numbers from 1 to 1000. Make it print only the sum, not the intermediate values.

public class Main { public static void main(String[] args) { int sum = 0; // Use a while loop to calculate the sum of 1 to 1000 System.out.println(sum); } }

17th Jun 2019, 7:08 PM
Arta
Arta - avatar
3 Answers
+ 4
https://www.sololearn.com/learn/Java/2146/?ref=app If you read the lesson about while loops it should be possible to solve your task by your own.
17th Jun 2019, 10:16 PM
Denise Roßberg
Denise Roßberg - avatar
+ 3
public class Main { public static void main(String[] args) { int sum = 0; int i=1; // Use a while loop to calculate the sum of 1 to 1000 while(i <=1000) {sum =sum+i; i++; } System.out.println(sum); } }
26th Dec 2020, 9:47 AM
بلوچ حوت حوت
بلوچ حوت حوت - avatar
0
I can't complete it
17th Jun 2019, 7:40 PM
Arta
Arta - avatar