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.
Java Recursion :
Recursion can be used to solve problems such as searching, sorting, and processing tree-like data structures. Recursion is often used to solve problems that can be broken down into smaller sub-problems that are similar in nature.
Java Methods
A method in Java is a block of code that performs a specific task. Methods help to break a program into smaller, reusable parts, making it easier to write and maintain complex programs. Methods can also accept input arguments and return a value, which can be used in other parts of the program.
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.
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
Explanation:
This program defines the sumOfNaturals method that calculates the sum of the first 'n' natural numbers using recursion.
The main method calls the sumOfNaturals method with a value of 5 for 'n', and prints the result.
You can change the value of 'n' to find the sum of the first 'n' natural numbers for any positive integer value of 'n'.
Example 2 :Java Program to Calculate the Sum of Natural Numbers using Recursion
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
Explanation:
This program defines the sumOfNaturals method that calculates the sum of the first 'n' natural numbers using recursion.
The main method calls the sumOfNaturals method with a value of 500 for 'n', and prints the result.
You can change the value of 'n' to find the sum of the first 'n' natural numbers for any positive integer value of 'n'.
Example 3: Java Program to Calculate the Sum of Natural Numbers using Recursion.
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
Explanation:
This program defines the sumOfNaturals method that calculates the sum of the first 'n' natural numbers using recursion.
The main method calls the sumOfNaturals method with a value of 5 for 'n', and prints the result.
You can change the value of 'n' to find the sum of the first 'n' natural numbers for any positive integer value of 'n'.
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
Login/Signup to comment