Java Program to Find the sum of First N natural Number
In this article, we will see Java Program to Find the Sum of First N Natural Numbers. A Natural number is the same as a Counting number. They are used to Count the number of real physical objects. Natural numbers start from 1 and go on infinite.
The positive numbers 1,2,3 ... are known as natural numbers.
The formula for Sum of First n natural numbers is n(n+1)/2.
If you want to add the first 5 Natural numbers then we find the Sum of 1+2+3+4+5 =15.
Declare and initialize variable, i,num, s=0.
Enter the value of num i.e. number up to which sum is to be calculated.
Use Loop to add numbers. Any of while, do-while or for loop can be used.
Print the sum.
public class Main
public static void main (Stringargs)
int n = 10;
int sum = 0;
for (int i = 1; i <= n; i++)
sum += i;
Sum is 15