Sum of N Natural Numbers in Java

We have to develop a Java program to calculate the sum of n natural numbers. Sum of natural number N as given as sum = 1+2+3+….+N

Examples:-
1+2+3+4+5 = 15
1+2+3+4+5+6+7+8+9+10 = 55

Procedure to develop a method to find the sum of N natural numbers in Java,

  1. Take the N value.
  2. Declare an iterator variable and initialize it with 1 because natural numbers start with 1.
  3. Add iterator variable value into the sum variable
  4. Increase the value of the iterator variable by 1
  5. Repeat 3 and 4 steps until the number remains greater than the iterator variable

The time complexity of this procedure is O(n).

import java.util.Scanner;

public class NaturalNumberSum {

  // method to find sum of N natural numbers
  public static int naturalNumberSum(int number){

     int i = 1; // iterator variable
     // variable to store sum value
     int sum = 0;

     // loop to repeat the process
     while (i<=number) {

        // add into sum value
        sum = sum + i;
        // increase iterator variable
        i++;
     }

     // return sum value
     return sum;
  }

  public static void main(String[] args) {

     // declare variables
     int number = 0;
     int sum = 0;

     // create Scanner class object
     Scanner scan = new Scanner(System.in);

     // read input
     System.out.print("Enter N value:: ");
     number = scan.nextInt();

     // Calculate the sum value
     sum = naturalNumberSum(number);

     // display result
     System.out.println("Sum = "+sum);

     // close Scanner class objects
     scan.close();
  }
}

The output for different test-cases:-

Enter N value:: 5
Sum = 15

Enter N value:: 10
Sum = 55

In this program, we have used a while loop to find the sum of natural numbers in Java. While loop is a pre-test loop where the expression is evaluated then only statements are executed. It uses a test expression to control the loop. Before every iteration of the loop, the test expression is evaluated.

Also See:-

We can also use for loop instead of using a while loop. The for loop is also a pre-test loop, where first of all initialization expression is evaluated then the condition is checked and if the condition is true then only the statements of the for loop are executed.

public static int naturalNumberSum(int number){

     int sum = 0;

     for(int i=1; i<=number; i++)
         sum+=i;

     return sum;
 }

Or,

public static int naturalNumberSum(int number){

  int sum = 0;

  for(int i=1; ; sum+=i, i++) 
    if(i>number) return sum;

}

The time complexity of all above methods are O(n).

Sum of Natural Numbers in Java without using the loop

We can also do the same work without using the loop. The formula for this operation,

Sum = n * (n+1) / 2;

Example:-
Sum of first 10 natural numbers = 10*(10+1)/2 = 10*11/2 = 5*11 = 55

It is the best way to find the sum of natural numbers. The time complexity of this method is O(1).

import java.util.Scanner;

public class NaturalNumberSum {

   // method to find sum of N natural numbers
   public static int naturalNumberSum(int number){
      return number*(number+1)/2;
   }

   public static void main(String[] args) {

      // declare variables
      int number = 0;
      int sum = 0;

      // create Scanner class object
      Scanner scan = new Scanner(System.in);

      // read input
      System.out.print("Enter N value:: ");
      number = scan.nextInt();

      // Calculate the sum value
      sum = naturalNumberSum(number);

      // display result
      System.out.println("Sum = "+sum);

      // close Scanner class objects
      scan.close();
   }
}

Using recursion

We already developed java program to find the sum of the natural number using for loop, or while loop, or without using the loop. Now we will find the same using the recursion technique. In Recursion, We divided the big problems into smaller problems or sub-problems.

Sum of N natural numbers given as 1+2+….+(n-1)+n. So, the problem can be divided as n + ( (n-1) +… + 2 + 1 )

General case for finding the sum of natural number => sum(n) = n + sum(n-1); Similarly, the base case for finding the sum of natural number => sum(0) = 0; or sum(1) = 1;

import java.util.Scanner;

public class NaturalNumberSum {

   // method to find sum of N natural numbers
   private static int naturalNumberSum(int number) {

      if(number==1) return 1; // base case

      else // general case
      return number + naturalNumberSum(number-1);
   }

   public static void main(String[] args) {

      // declare variables
      int number = 0;
      int sum = 0;

      // create Scanner class object
      Scanner scan = new Scanner(System.in);

      // read input
      System.out.print("Enter N value:: ");
      number = scan.nextInt();

      // Calculate the sum value
      sum = naturalNumberSum(number);

      // display result
      System.out.println("Sum = "+sum);

      // close Scanner class objects
      scan.close();
   }
}

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Reply