Example 45: Sum of the First n Natural Numbers
Learn how to calculate the sum of the first n natural numbers using recursion.
We'll cover the following
Problem
Write a recursive function to obtain the running sum of the first n natural numbers.
Example
Input | Output |
---|---|
5 | 15 |
25 | 325 |
70 | 2485 |
Try it yourself
Try to solve this question on your own in the code widget below. If you get stuck, you can always refer to the solution provided.
Level up your interview prep. Join Educative to access 80+ hands-on prep courses.