Example 45: Sum of the First n Natural Numbers

Learn how to calculate the sum of the first n natural numbers using recursion.

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.

Get hands-on with 1400+ tech skills courses.