by Dinesh Thakur

In this example, the sum of first 10 natural numbers is displayed. First, input the value of n (10 in this case) i.e. number of natural numbers whose sum is to be calculated. Then, after initializing the variables i to 1 and sum to 0, we enter the do-while loop. The execution of the body of loop continues as long as condition (i<=n) evaluates to true. When variable 1’s is value becomes 11, the condition becomes false and this terminates the do-while loop and program execution continues with the next statement after the loop which displays the sum of first 10 natural numbers.

//Program to Find Sum of First N Natural Numbers

 import java.util.Scanner; //Program uses Scanner class
public class SumNatural
     public static void main(String[] args)
        int n,i=1,sum=0;
        Scanner input=new Scanner(;
        System.out.print("Enter Number :");
                 i +=1;
            }   while(i<=n);
                 System.out.print("Sum of First " + n + " Numbers = "+sum);

Sum of First N Natural Numbers