# Java Program to Find Sum of Natural Numbers

The natural numbers are the numbers that include all the positive integers from 1 to infinity. For example, 1, 2, 3, 4, 5, ……, n. When we add these numbers together, we get the sum of natural numbers.

In this section, we will create the following programs:

• Java program to find the sum of the first 100 natural numbers
• Java program to find the sum of n natural numbers
• Java program to find the sum of n natural numbers using the function

We can also find the sum of n natural number using the mathematical formula:Sum of n natural numbers=n*(n+1)/2

Suppose, we want to find the sum of the first 100 natural numbers. By putting the value in the above formula, we get:

In this section, we are going to use the following ways to find the sum of natural numbers.

• Using Java for Loop
• Using Java while Loop
• Using Function

## Using Java for Loop

Using Java for loop is the easiest way to find the sum of natural numbers.

SumOfNaturalNumber1.java

1. public class SumOfNaturalNumber1
2. {
3. public static void main(String[] args)
4. {
5. int i, num = 10, sum = 0;
6. //executes until the condition returns true
7. for(i = 1; i <= num; ++i)
8. {
9. //adding the value of i into sum variable
10. sum = sum + i;
11. }
12. //prints the sum
13. System.out.println(“Sum of First 10 Natural Numbers is = ” + sum);
14. }
15. }

Output:

```Sum of First 10 Natural Numbers is = 55
```

## Using Java while Loop

In the following example, we have replaced the for loop with the while loop. The while loop executes until the condition i <= num do not become false. It calculates the sum of natural numbers up to a specified limit.

SumOfNaturalNumber2.java

1. public class SumOfNaturalNumber2
2. {
3. public static void main(String[] args)
4. {
5. int num = 100, i = 1, sum = 0;
6. //executes until the condition returns true
7. while(i <= num)
8. {
9. //adding the value of i into sum variable
10. sum = sum + i;
11. //increments the value of i by 1
12. i++;
13. }
14. //prints the sum
15. System.out.println(“Sum of First 100 Natural Numbers is = ” + sum);
16. }
17. }

Output:

```Sum of First 100 Natural Numbers is = 5050
```

## Sum of n Natural Numbers

The following program finds the sum of n natural numbers. In this program, we have used the same while loop, as we have used in the above program. We have also taken two inputs from the user i.e. i and num. The variable i is the starting number and the variable num is the end number. For example, if we want to find the sum of natural numbers from 20 (i) to 100 (num).

SumOfNaturalNumber3.java

1. import java.util.Scanner;
2. public class SumOfNaturalNumber3
3. {
4. public static void main(String[] args)
5. {
6. int num, i, sum = 0;
7. //object of Scanner class
8. Scanner sc = new Scanner(System.in);
9. System.out.print(“Sum from: “);
10. //takes an integer as input
11. i = sc.nextInt();
12. System.out.print(“Sum up to: “);
13. //takes an integer as input
14. num = sc.nextInt();
15. while(i <= num)
16. {
17. //adding the value of i into sum variable
18. sum = sum + i;
19. //increments the value of i by 1
20. i++;
21. }
22. //prints the sum
23. System.out.println(“Sum of Natural Numbers = ” + sum);
24. }
25. }

Output:

```Sum from: 1
Sum up to: 1000
Sum of Natural Numbers = 500500
```

## Using Function

In the following program, we have found the sum of n natural number using the function.

SumOfNaturalNumber4.java

1. public class SumOfNaturalNumber4
2. {
3. //method that returns the sum of n natural numbers
4. static int naturalNumberSum(int n)
5. {
6. int sum = 0;
7. //executes until the condition becomes false
8. for (int i = 1; i <= n; i++)
9. //adding the value of i to the sum variable
10. sum = sum + i;
11. return sum;
12. }
13. //main method
14. public static void main(String args[])
15. {
16. int n = 50;
17. //calling method and prints the sum
18. System.out.println(“Sum of Natural Numbers is: “+naturalNumberSum(n));
19. }
20. }

Output:

```Sum of Natural Numbers is: 1275
```

Let’s see another program.

In the following program, we have used the formula to find the sum of natural numbers.

SumOfNaturalNumber5.java

1. public class SumOfNaturalNumber5
2. {
3. //method that returns the sums
4. static int sumOfNaturalNumbers(int n)
5. {
6. //formula to calculate the sum of natural numbers
7. return n * (n + 1) / 2;
8. }
9. //main method
10. public static void main(String args[])
11. {
12. int n = 5;
13. //calling the method and printing the sum
14. System.out.println(“Sum of Natural Numbers is: “+sumOfNaturalNumbers(n));
15. }
16. }

Output:

`Sum of Natural Number is: 20100`