Find the Sum of N Natural Numbers in Java

Find the Sum of N Natural Numbers in Java

Find the Sum of N Natural Numbers in Java

Given an integer input “num” the objective is to sum up all the numbers that lay in the interval [0,num]. To do so we’ll write a code to Find the Sum of N Natural Numbers in Java.

Example
Input : 6
Output : 21
Explanation : 0 + 1 + 2 + 3 + 4 + 5 + 6 = 21.

Find the Sum of N Natural Numbers in Java

Given an integer num input as the upper limit, the objective is to sum up all the numbers that lay in the interval starting from 0 to the integer input “num”. Here are some of the methods to sum up all the numbers in a given interval

  • Method 1: Using for Loop
  • Method 2: Using Formula
  • Method 3: Using Recursion

We’ll discuss the above mentioned methods in detail in the sections below.

Method 1: Using for Loop

In this method we’ll use for loop to iterate through the numbers in a range of [0,num].

Working

For an integer input “num” we do the following

  • initialize the required variables within the class.
  • Run a for loop from 0 to num+1 meanwhile appending all the numbers to sum variable.
  • Print the sum variable.

Let’s implement the logic in Java Language.

Java Code

public class Main
 {
   public static void main (String[]args)
   {

     int n = 10;
     int sum = 0;

     for (int i = 1; i <= n; i++)
         sum += i;
       System.out.println (sum);
   }
 }

Output

55

Method 2: Using the Formula

In this method we’ll use a formula to find the sum of N integers in a series from sequence and series. The formula is mentioned below.

Working

For an integer input we do the following operations

  • Initialize the following variables.
  • Implement the formula as sum = num * ( num + 1 ) / 2.
  • Print the sum variable.

Let’s implement the above logic in Java language.

Java Code

public class Main
 {
   public static void main (String[]args)
   {

     int n = 10;

       System.out.println ( n*(n+1)/2);
   }
 }

Output

55

Method 3: Using Recursion

In this method we’ll use the concept of recursion to find the sum of all the number that lay from 0 to num. To know more about recursion, check out Recursion in Java

Working

For a given integer input as “num” we perform the following steps

  • Initialize the required variables.
  • Define a recursive function with base case as num == 0.
  • Set recursive step call as num + recursum(num-1).
  • Call the recursive function and print the value returned by the function.

Let’s implement the above logic in Java Language.

Java Code

public class Main
{
  public static void main (String[]args)
  {

    int n = 10;
    int sum = getSum (n);

      System.out.println (sum);
  }

  static int getSum (int n)
  {
    if (n == 0)
      return n;

    return n + getSum (n - 1);
  }
}

Output

55

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 150+ courses offered by PrepInsta in One Subscription

Get over 150+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

Getting Started