Java Program to find Sum of N Natural Numbers

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

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

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

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

User entered value: number  = 6

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

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

Second Iteration: (i = 2; 2 <= 6; 2++). So, sum = 1 + 2 = 3

Third Iteration: (i = 3; 3 <= 6; 3++). So, sum = 3 + 3 = 6

Fourth Iteration: (i = 4; 4 <= 6; 4++). So, sum = 6 + 4 = 10

Fifth Iteration: (i = 5; 5 <= 6; 5++). So, sum = 10 + 5 = 15

Sixth Iteration: (i = 6; 6 <= 6; 6++). So, sum = 15 + 6 = 21

7th Iteration: (i = 7; 7 <= 6; 7++). The 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 program to calculate the sum of N natural numbers is the same as the above, but this time, we are using the While Loop.

import java.util.Scanner;

public class Example2 {
	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 = sc.nextInt();	
		
		while(i <= number)
		{
			sum = sum + i; 
			i++;
		}	
		
		System.out.println("\n The Sum of Natural Numbers from 1 to "+ number + " = " + sum);
	}
}
Java Program to find Sum of N Natural Numbers using While loop

Java Program to find Sum of N Natural Numbers using Functions

The mathematical formula behind the Sum of Series 1 + 2+ 3+ … + N = N * (N + 1) / 2. In this 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 whether 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

import java.util.Scanner;
public class Example3 {
	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 = SNat(number);
		System.out.println("\n The Sum of Natural Numbers from 1 to "+ number + " = " + sum);
	}
	
	public static int SNat(int num)
	{		
		if(num == 0)
		{
			return num;
		}
		else
		{
			return (num * (num + 1)/2);
		}
	}
}
Java Program to find Sum of N Natural Numbers using Functions

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 program, we recursively call the SoNat method with updated values.

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

The sum of Natural Numbers between a Range

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

import java.util.Scanner;
public class Example5 {
	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);
	}
}
 Please Enter the Minimum value : 5
 Please Enter the Maximum value : 100

 The Sum of Natural Numbers from 5 to 100 = 5040