Sum of natural numbers using recursion Last Updated : 17 Feb, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report Given a number n, find sum of first n natural numbers. To calculate the sum, we will use a recursive function recur_sum().Examples : Input : 3 Output : 6 Explanation : 1 + 2 + 3 = 6 Input : 5 Output : 15 Explanation : 1 + 2 + 3 + 4 + 5 = 15 Below is code to find the sum of natural numbers up to n using recursion : C++ // C++ program to find the // sum of natural numbers up // to n using recursion #include <iostream> using namespace std; // Returns sum of first // n natural numbers int recurSum(int n) { if (n <= 1) return n; return n + recurSum(n - 1); } // Driver code int main() { int n = 5; cout << recurSum(n); return 0; } Java // Java program to find the // sum of natural numbers up // to n using recursion import java.util.*; import java.lang.*; class GFG { // Returns sum of first // n natural numbers public static int recurSum(int n) { if (n <= 1) return n; return n + recurSum(n - 1); } // Driver code public static void main(String args[]) { int n = 5; System.out.println(recurSum(n)); } } // This code is contributed by Sachin Bisht Python # Python code to find sum # of natural numbers upto # n using recursion # Returns sum of first # n natural numbers def recurSum(n): if n <= 1: return n return n + recurSum(n - 1) # Driver code n = 5 print(recurSum(n)) C# // C# program to find the // sum of natural numbers // up to n using recursion using System; class GFG { // Returns sum of first // n natural numbers public static int recurSum(int n) { if (n <= 1) return n; return n + recurSum(n - 1); } // Driver code public static void Main() { int n = 5; Console.WriteLine(recurSum(n)); } } // This code is contributed by vt_m PHP <?php // PHP program to find the // sum of natural numbers // up to n using recursion // Returns sum of first // n natural numbers function recurSum($n) { if ($n <= 1) return $n; return $n + recurSum($n - 1); } // Driver code $n = 5; echo(recurSum($n)); // This code is contributed by Ajit. ?> JavaScript <script> // JavaScript program to find the // sum of natural numbers // up to n using recursion // Returns sum of first // n natural numbers function recurSum(n) { if (n <= 1) return n; return n + recurSum(n - 1); } // Driver code let n = 5; document.write(recurSum(n)); // This code is contributed by mohan </script> Output : 15 Time complexity : O(n) Auxiliary space : O(n) To solve this question , iterative approach is the best approach because it takes constant or O(1) auxiliary space and the time complexity will be same O(n). Comment More infoAdvertise with us Next Article What is Recursion? P Pushpanjali chauhan Follow Improve Article Tags : Misc Recursion DSA Basic Coding Problems Practice Tags : MiscRecursion Similar Reads Introduction to Recursion The process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution 14 min read What is Recursion? Recursion is defined as a process which calls itself directly or indirectly and the corresponding function is called a recursive function.Example 1 : Sum of Natural Numbers Let us consider a problem to find the sum of natural numbers, there are several ways of doing that but the simplest approach is 8 min read Difference between Recursion and Iteration A program is called recursive when an entity calls itself. A program is called iterative when there is a loop (or repetition).Example: Program to find the factorial of a number C++#include<iostream> using namespace std; // ----- Recursion ----- // method to find // factorial of given number in 5 min read Types of Recursions What is Recursion? The process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. Using recursive algorithm, certain problems can be solved quite easily. Examples of such problems are Towers of Hanoi (TOH), Inord 15+ min read Finite and Infinite Recursion with examples The process in which a function calls itself directly or indirectly is called Recursion and the corresponding function is called a Recursive function. Using Recursion, certain problems can be solved quite easily. Examples of such problems are Towers of Hanoi (TOH), Inorder/Preorder/Postorder Tree Tr 6 min read What is Tail Recursion Tail recursion is defined as a recursive function in which the recursive call is the last statement that is executed by the function. So basically nothing is left to execute after the recursion call.For example the following function print() is tail recursive.C++// An example of tail recursive funct 7 min read What is Implicit recursion? What is Recursion? Recursion is a programming approach where a function repeats an action by calling itself, either directly or indirectly. This enables the function to continue performing the action until a particular condition is satisfied, such as when a particular value is reached or another con 5 min read Why is Tail Recursion optimization faster than normal Recursion? Tail recursion is defined as a recursive function in which the recursive call is the last statement that is executed by the function. So basically nothing is left to execute after the recursion call.Why is tail recursion optimization faster than normal recursion?In non-tail recursive functions, afte 4 min read Recursive Functions A Recursive function can be defined as a routine that calls itself directly or indirectly. In other words, a recursive function is a function that solves a problem by solving smaller instances of the same problem. This technique is commonly used in programming to solve problems that can be broken do 4 min read Difference Between Recursion and Induction Recursion and induction are fundamental ideas in computer science and mathematics that might be regularly used to solve problems regarding repetitive structures. Recursion is a programming technique in which a function calls itself to solve the problem, whilst induction is a mathematical proof techn 4 min read Like