Open In App

Java Program to Calculate Sum of First N Natural Numbers

Last Updated : 12 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will check How to calculate the sum of N natural numbers using an iterative approach i.e. using a for a loop and Mathematical Formulae.

Example to Find Sum of N Natural Numbers:

Input: N = 10
Output: Sum of first 10 Natural Number = 55

Input: N = 5
Output: Sum of first 5 Natural Number = 15

Methods to Find the Sum of N Natural Numbers in Java

There are two methods to find the sum of N Natural Numbers as mentioned below:

  1. Using Loops
  2. Using Mathematical Formulae

1. Using Loop

Using Loop is one of the most logical methods to find the sum of N natural numbers as we need to just iterate from 1 to N numbers to get the sum. Let us check the approach to implementing the Method.

Note: In this method, we are using for loop but you can use while, do-while too. For loop has three parameters initialization, testing condition, and increment/decrement.

Approach

  1. Start for loop initialization with i = 1.
  2. Write the testing condition as i <= N.
  3. Add increment statement as i++ or i+=1.
  4. Initialize a variable sum with 0.
  5. Start adding i with the sum at each iteration of for loop.
  6. Print sum at the end for loop.

Below is the implementation of above approach:

Java




// Java Program to Display Numbers
// from 1 to N Using For Loop and
// sum of First N Natural Number
import java.io.*;
  
// Driver Class
class GFG {
      // main function
    public static void main(String[] args)
    {
        int N = 10;
        int sum = 0;
        System.out.println("Finding Sum of " + N + " numbers using for loop");
  
        // we initialize the value of the variable i 
        // with 1 and increment each time with 1
        for (int i = 1; i <= N; i++) {
            sum += i;
        }
        
        System.out.println("Sum of first " + N
                           + " Natural Number = " + sum);
    }
}


Output

Finding Sum of 10 numbers using for loop

Sum of first 10 Natural Number = 55

Complexity of the above method:

Time Complexity: O(n)
Auxiliary Space: O(1) because constant space for variables is being used

2. Using Mathematical Formulae

Maths and it’s application is all we need to find the sum of Numbers. Let us check the Formulae and the approach before checking on the Program.

Mathematical Formulae:

Sum = N*(N+1)/2

Approach

  1. Start for loop initialization with i = 1.
  2. Write testing condition as i <= N.
  3. Add increment statement as i++ or i+=1.
  4. Start Printing i for each iteration.
  5. Print sum using first N natural number formula.

Below is the implementation of the above approach:

Java




// Java Program to find the Sum
// Using Mathematical Formulae
import java.io.*;
  
// Driver Class
class GFG {
    // main function
    public static void main(String[] args)
    {
        int N = 5;
        System.out.println("Finding Sum of " + N + " Numbers using Mathematical Formulae");
  
        // Calculating the sum
        int sum = (N*(N+1))/2;
  
        // Pring the result
        System.out.println("Sum of first " + N
                           + " Natural Number = " + sum);
    }
}


Output

Finding Sum of 5 Numbers using Mathematical Formulae
Sum of first 5 Natural Number = 15

Complexity of the above method:

Time Complexity: O(1) for Finding the Sum of elements
Auxiliary Space: O(1) as it is using constant space for variables



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads