44.6k views
10 votes
Write a java program named SumThousand The program adds all the number from 1 to 1000 together and print out the sum

User Sixhobbits
by
4.3k points

1 Answer

5 votes

Answer:

The program in Java is as follows:

public class SumThousand {

public static void main(String args[]) {

int sum=0;

for(int i =1;i<=1000;i++)

sum+=i;

System.out.println("Sum = " + sum);

}

}

Step-by-step explanation:

This initializes sum to 0

int sum=0;

This iterates from 1 to 1000

for(int i =1;i<=1000;i++)

This calculates the required sum

sum+=i;

This prints the calculated sum

System.out.println("Sum = " + sum);

User Djvg
by
3.8k points