Java Program to find Sum of N Natural Numbers

Write a Java Program to find Sum of N Natural Numbers using For Loop, and While Loop with example.

Java Program to find Sum of N Natural Numbers using For loop

This program allows the user to enter any integer value (maximum limit value). Next, this Java program calculates the sum of all natural numbers from 1 to maximum limit value using For Loop.

// Java Program to find Sum of N Natural Numbers
import java.util.Scanner;

public class SumOfNaturalNumbers1 {
	private static Scanner sc;
	public static void main(String[] args) 
	{
		int number, i, sum = 0;
		sc = new Scanner(System.in);
		
		System.out.print(" Please Enter any Number : ");
		number = sc.nextInt();	
		
		for(i = 1; i <= number; i++)
		{
			sum = sum + i; 
		}	
		
		System.out.println("\n The Sum of Natural Numbers from 1 to "+ number + " = " + sum);
	}
}
Java Program to find Sum of N Natural Numbers 1

First, we used the For loop to iterate from 1 to maximum value (Here, number = 6).

User entered value: number  = 6

For Loop First Iteration: for(i = 1; i <= 6; i++)
Condition is True.
sum = sum + i
sum = 0 + 1 = 1

Second Iteration: for(i = 2; 2 <= 6; 2++)
sum = 1 + 2 = 3

Third Iteration: for(i = 3; 3 <= 6; 3++)
sum = 3 + 3 = 6

Fourth Iteration: for(i = 4; 4 <= 6; 4++)
sum = 6 + 4 = 10

Fifth Iteration: for(i = 5; 5 <= 6; 5++)
sum = 10 + 5 = 15

Sixth Iteration: for(i = 6; 6 <= 6; 6++)
sum = 15 + 6 = 21

7th Iteration: for(i = 7; 7 <= 6; 7++)
Condition (7 <= 6) is False. So, the Java compiler exits from the For Loop.

Java Program to find Sum of N Natural Numbers using While loop

This Java program to calculate the sum of N natural numbers is the same as the above, but this time we are using the While Loop.

// Java Program to find Sum of N natural Numbers
import java.util.Scanner;

public class SumOfNaturalNumbers2 {
	private static Scanner sc;
	public static void main(String[] args) 
	{
		int number, i = 1, sum = 0;
		sc = new Scanner(System.in);
		
		System.out.print(" Please Enter any Number : ");
		number = sc.nextInt();	
		
		while(i <= number)
		{
			sum = sum + i; 
			i++;
		}	
		
		System.out.println("\n The Sum of Natural Numbers from 1 to "+ number + " = " + sum);
	}
}

Java Sum of Natural Numbers using a While loop output

 Please Enter any Number : 10

 The Sum of Natural Numbers from 1 to 10 = 55

Java Program to find Sum of N Natural Numbers using Method

The mathematical formula behind the Sum of Series 1 + 2+ 3+ … + N = N * (N + 1) / 2. In this Java program, we are creating a separate method to calculate the sum of natural numbers.

Within the function, we used the If Else statement to check the Number is equal to Zero or not. If it is True, the Sum of N Natural numbers = 0. Otherwise, it executes N * (N + 1) / 2

// Java Program to find Sum of N natural Numbers
import java.util.Scanner;
public class SumOfNaturalNumbers3 {
	private static Scanner sc;
	public static void main(String[] args) 
	{
		int number, sum = 0;
		sc = new Scanner(System.in);
		
		System.out.print(" Please Enter any Number : ");
		number = sc.nextInt();	
		
		sum = SumOfNaturalNumbers(number);
		System.out.println("\n The Sum of Natural Numbers from 1 to "+ number + " = " + sum);
	}
	
	public static int SumOfNaturalNumbers(int num)
	{		
		if(num == 0)
		{
			return num;
		}
		else
		{
			return (num * (num + 1)/2);
		}
	}
}

Java Sum of Natural Numbers using functions output

 Please Enter any Number : 30

 The Sum of Natural Numbers from 1 to 30 = 465

Java Program to Calculate Sum of N Natural Numbers using Recursive Method

This sum of natural numbers program is the same as the above example. But in this Java program, we are recursively calling the SumOfNaturalNumbers method with updated values.

// Java Program to find Sum of N natural Numbers
import java.util.Scanner;
public class SumOfNaturalNumbers4 {
	private static Scanner sc;
	public static void main(String[] args) 
	{
		int number, sum = 0;
		sc = new Scanner(System.in);
		
		System.out.print(" Please Enter any Number : ");
		number = sc.nextInt();	
		
		sum = SumOfNaturalNumbers(number);
		System.out.println("\n The Sum of Natural Numbers from 1 to "+ number + " = " + sum);
	}
	public static int SumOfNaturalNumbers(int num)
	{		
		if(num == 0)
		{
			return num;
		}
		else
		{
			return (num + SumOfNaturalNumbers(num - 1));
		}
	}
}

Java recursive function to get Sum of Natural Numbers output

 Please Enter any Number : 80

 The Sum of Natural Numbers from 1 to 80 = 3240

Java Program to Calculate Sum of N Natural Numbers between a Range

This Java program allows entering Minimum and maximum value. Next, the Java program finds the sum of natural numbers between Minimum value and maximum value.

// Java Program to find Sum of N natural Numbers
import java.util.Scanner;
public class SumOfNaturalNumbers5 {
	private static Scanner sc;
	public static void main(String[] args) 
	{
		int minimum, maximum, i, sum = 0;
		sc = new Scanner(System.in);
		
		System.out.print(" Please Enter the Minimum value : ");
		minimum = sc.nextInt();			
		
		System.out.print(" Please Enter the Maximum value : ");
		maximum = sc.nextInt();	
		
		for(i = minimum; i <= maximum; i++)
		{
			sum = sum + i; 
		}	
		
		System.out.println("\n The Sum of Natural Numbers from " + minimum + " to "+ maximum + " = " + sum);
	}
}

Java sum of Natural Numbers between 1 and 1000 output

 Please Enter the Minimum value : 5
 Please Enter the Maximum value : 100

 The Sum of Natural Numbers from 5 to 100 = 5040