Java Program to Find the Sum of Natural Numbers using Recursion

Java Program to Find the Sum of Natural Numbers using Recursion

Java Recursion

  • Recursion is a technique in computer programming where a function calls itself to solve a problem.
  • In Java, recursion is often used in conjunction with the Stack data structure to implement algorithms such as depth-first search and breadth-first search.
  • You must be familiar with following topics to understand the correspond example Such as: Java Recursion and Java Methods.
  • To understand the how to Calculate the sum of Natural Numbers using Recursion , Read the Complete Article.

Steps to find the sum of the first ‘n’ natural numbers using recursion in Java Program:

  • Here are the Steps to Calculate the Sum of Natural Numbers in Java Program using Recursion:, You can follow these steps:
    • Define a static method that takes an integer ‘n’ as an argument.
    • Check the base case: If ‘n’ is equal to 1, return 1 as the sum of the first natural number.
    • Recursively call the method with ‘n-1’ as the argument.
    • In the recursive call, add the returned value to ‘n’.
    • Return the sum.

Let’s look at the Java Program to Calculate the Sum of Natural Numbers using Recursion to perform certain operations.

Example 1: Java Program to Calculate the Sum of Natural Numbers using Recursion.

Run

public class Main
{
  public static int sumOfNaturals (int n)
  {
    if (n == 1)
      {
	return 1;
      }
    else
      {
	return n + sumOfNaturals (n - 1);
      }
  }

  public static void main (String[]args)
  {
    int n = 5;
    int sum = sumOfNaturals (n);
    System.out.println ("The sum of the first " + n +
			" natural numbers is: " + sum);
  }
}

Output

The sum of the first 5 natural numbers is: 15

Example 2 :Java Program to Calculate the Sum of Natural Numbers using Recursion

Run

public class Main
{
  public static int sumOfNaturals (int n)
  {
    if (n == 1)
      {
	return 1;
      }
    else
      {
	return n + sumOfNaturals (n - 1);
      }
  }

  public static void main (String[]args)
  {
    int n = 500;
    int sum = sumOfNaturals (n);
    System.out.println ("The sum of the first " + n +
			" natural numbers is: " + sum);
  }
}

Output

The sum of the first 500 natural numbers is: 125250

Example 3: Java Program to Calculate the Sum of Natural Numbers using Recursion. 

Run

public class Main {
    public static int sumOfNaturals(int n) {
        if (n == 1) {
            return 1;
        } else {
            return n + sumOfNaturals(n-1);
        }
    }

    public static void main(String[] args) {
        int n = 99;
        int sum = sumOfNaturals(n);
        System.out.println("The sum of the first " + n + " natural numbers is: " + sum);
    }
}

Output

The sum of the first 99 natural numbers is: 4950

Prime Course Trailer

Related Banners

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

Get over 200+ 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