Android IDE (AIDE) Support Forum

I want to 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);
}

}

Hi :slight_smile: that’s easy. You do the calculation inside the loop but you put the print statement below it, outside the loop. Then when the loop is finished and it has added up the total, the loop exits and the line that says print is executed, just once.

I can not find it yet. Please write it down to me by the Java language script. Please write inside and outside the loop.

1 Like

1 Like