C Program
#include <stdio.h> int sum(int n) { if(n == 0) return 0; return n + sum(n - 1); } int main() { int n = 5; printf("Sum of first %d numbers is %d\n", n, sum(n)); return 0; }
C Output
Input: 5 Output: Sum of first 5 numbers is 15
C++ Program
#include <iostream> using namespace std; int sum(int n) { if(n == 0) return 0; return n + sum(n - 1); } int main() { int n = 7; cout << "Sum of first " << n << " numbers is " << sum(n); return 0; }
C++ Output
Input: 7 Output: Sum of first 7 numbers is 28
JAVA Program
public class Main { static int sum(int n) { if(n == 0) return 0; return n + sum(n - 1); } public static void main(String[] args) { int n = 4; System.out.println("Sum of first " + n + " numbers is " + sum(n)); } }
JAVA Output
Input: 4 Output: Sum of first 4 numbers is 10
Python Program
def sum_n(n): if n == 0: return 0 return n + sum_n(n - 1) n = 6 print(f"Sum of first {n} numbers is {sum_n(n)}")
Python Output
Input: 6 Output: Sum of first 6 numbers is 21
Explanation
Example
When you are looking for the sum of the first n natural numbers, you may add them individually or utilize recursion to divide the problem into smaller, same problems. For instance, the first 5 numbers' sum can be written as 5 + first 4 numbers' sum, and similarly, it continues until you get to 0, where it is just 0. Recursive function continues to decrease n until it reaches the base case, and then the results are combined together as the recursion unfolds.
Real-Life Analogy
Suppose you have a pile of coins with each coin stamped with a value from 1 to n. You would like to know the total value, but rather than counting them one by one, you take the top coin, record its value, and pass the remainder of the stack to someone with the same request. They repeat the process until there is no coin left. When the last person reaches an empty stack (the base case), they return 0, and the values of each coin are summed on the return journey.
Why It Matters
This exercise illustrates how recursion can be used to eliminate loops for sequential computations. It shows how to recognize a base case, call the function recursively with a smaller problem size, and glue results together to produce the final solution. Familiarity with such problems develops the thought process required to understand more complicated recursive algorithms such as tree traversals, divide-and-conquer sorting, and backtracking.
Complexity and Optimization
Time complexity is O(n) as the function executes once for every number from n down to 0. The space complexity also is O(n) because of the recursive call stack. Although recursion is beautiful, for very large n it is less efficient than applying the mathematical formula n * (n + 1) / 2, which executes in O(1) time and O(1) space. Recursion is priceless to develop conceptual understanding.
Edge Cases and Testing
The minimum valid input is 0, and this must return a sum of 0. Inputs such as 1 serve to check that the function supports minimal cases. Negative numbers don't work for the sum of natural numbers, so such cases must be excluded or handled in some separate way.
Interview Tips
Interviewers usually begin with this kind of problem to test whether you know recursion basics. They may follow up with an iterative solution, a formula-based solution, or extensions such as summing even numbers, odd numbers, or numbers between certain limits. Showing multiple solutions indicates flexibility in problem-solving and awareness of efficiency.
SEO-Friendly Closing
Recursion sum of N numbers is among the easiest yet most effective starter programming tasks in C, C++, Java, and Python. It constructs good knowledge of base cases, recursive procedures, and problem breaking down. With this example, programmers become confident about recursion that can be utilized in complex tasks such as data structure manipulation, mathematical computations, and algorithm design. This concept appears often in coding interviews, algorithm challenges, and academic exercises, making it an essential stepping stone for mastering programming logic.
Social Plugin