write a java program to find the sum of natural numbers from 1 to 1000 inclusive (using for loop)
sample output:
sum=500500
The program find the sum of natural number from 1 to 1000:
Code:
public class SumNatural {
public static void main(String[] args) {
int num = 1000, sum = 0;
for(int i = 1; i <= num; ++i)
{
// sum = sum + i;
sum += i;
}
System.out.println("Sum = " + sum);
}
}
-----------------------------------------------------------------
The code finds out the sum of the natural numbers from 1 to 1000
Output is : 500500
---------------------------------------------------------Please Upvote--------------------------------------------------------------------------
Get Answers For Free
Most questions answered within 1 hours.