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

User entered value: number  = 6

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

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

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

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

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

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

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

7th Iteration: (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.

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);
	}
}
 Please Enter any : 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

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);
		}
	}
}
 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 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));
		}
	}
}
 Please Enter any : 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.

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