Sum of Natural Numbers Using Recursion

In this example, you'll learn to find the sum of natural numbers using recursion. To solve this problem, a recursive function calculate_sum() function is created.

To understand this example, you should have the knowledge of the following R programming topics:

Example: Sum of Natural Numbers

# Program to find the sum of natural numbers upto n using recursion



> calculate_sum(7)
[1] 28

Here, we ask the user for a number. The number entered by the user is passed to calculate_sum() recursive function which computes the sum up to that number.

Did you find this article helpful?