Sum of First N Natural Numbers Using Recursion Last Updated : 27 Sep, 2025 Suggest changes Share 32 Likes Like Report Given a number n, find the sum of the first n natural numbers using recursion.Examples:Input: n = 3Output: 6Explanation: 1 + 2 + 3 = 6Input: n = 5Output: 15Explanation: 1 + 2 + 3 + 4 + 5 = 15Approach:To find the sum of the first n natural numbers using recursion, we define a function recurSum(n-1).At each step, the function adds the current number n to the sum of all smaller numbers by calling recurSum(n-1).The recursion continues until the base case is reached, where n = 0, at which point the function returns 0 to stop further calls.As the recursive calls return back, the intermediate sums are combined step by step, eventually producing the total sum of the first n natural numbers. C++ #include <iostream> using namespace std; int recurSum(int n){ // base case if (n == 0) return 0; // recursive case return n + recurSum(n - 1); } int main(){ int n = 3; cout << recurSum(n); return 0; } Java class GFG { public static int recurSum(int n){ // base case if (n == 0) return 0; // recursive case return n + recurSum(n - 1); } public static void main(String args[]){ int n = 3; System.out.println(recurSum(n)); } } Python def recurSum(n): # base case if n == 0: return 0 # recursive case return n + recurSum(n - 1) # Driver code if __name__ == "__main__": n = 3 print(recurSum(n)) C# using System; class GFG { public static int recurSum(int n){ // base case if (n == 0) return 0; // recursive case return n + recurSum(n - 1); } public static void Main(){ int n = 3; Console.WriteLine(recurSum(n)); } } JavaScript function recurSum(n) { // base case if (n == 0) return 0; // recursive case return n + recurSum(n - 1); } // Driver code let n = 3; console.log(recurSum(n)); Output6Time Complexity: O(n)Space Complexity: O(n) Recursive Space P Pushpanjali chauhan Follow 32 Article Tags : Misc Recursion DSA Basic Coding Problems Explore DSA FundamentalsLogic Building Problems 2 min read Analysis of Algorithms 1 min read Data StructuresArray Data Structure 3 min read String in Data Structure 2 min read Hashing in Data Structure 2 min read Linked List Data Structure 2 min read Stack Data Structure 2 min read Queue Data Structure 2 min read Tree Data Structure 2 min read Graph Data Structure 3 min read Trie Data Structure 15+ min read AlgorithmsSearching Algorithms 2 min read Sorting Algorithms 3 min read Introduction to Recursion 14 min read Greedy Algorithms 3 min read Graph Algorithms 3 min read Dynamic Programming or DP 3 min read Bitwise Algorithms 4 min read AdvancedSegment Tree 2 min read Binary Indexed Tree or Fenwick Tree 15 min read Square Root (Sqrt) Decomposition Algorithm 15+ min read Binary Lifting 15+ min read Geometry 2 min read Interview PreparationInterview Corner 3 min read GfG160 3 min read Practice ProblemGeeksforGeeks Practice - Leading Online Coding Platform 6 min read Problem of The Day - Develop the Habit of Coding 5 min read My Profile ${profileImgHtml} My Profile Edit Profile My Courses Join Community Transactions Logout Like