![]() |
VOOZH | about |
Natural numbers include all positive integers from 1 to infinity. There are multiple methods to find the sum of natural numbers and here, we will see how to find the sum of natural numbers using recursion.
Example
Input : 5
Output : 15
Explanation : 1 + 2 + 3 + 4 + 5 = 15Input : 10
Output : 55
Explanation : 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 = 55
Below is the implementation using recursion:
Sum = 55
Time complexity: O(n).
Auxiliary space: O(n).