Find Sum of Natural Numbers in Python Using Recursion

In this tutorial, we will learn how to find the sum of natural numbers in Python using recursion. Recursion is a technique in computer programming where a function calls itself until a condition is met. In this case, we will be using recursion to find the sum of natural numbers.

Python Program to Find the Sum of Natural Numbers Using Recursion

This is the main function rsum that will find the sum of natural numbers using recursion. The function takes an integer n as an input argument. The function starts with an if-else statement:

  • If the value of “n” is less than or equal to “1”, the function returns n.
  • If the value of “n” is greater than “1”, the function returns “n” plus the result of the function “rsum” with an argument “n-1”.

Here, we are taking the input from the user and converting it from string to integer using the “int()” function.

The input number is passed as an argument to the “rsum” function, and the result is stored in the variable “TTL”.

Finally, we print the sum of natural numbers, which is stored in the variable “TTL”.

# Find Sum of Natural Numbers in Python Using Recursion

def rsum(n):
	if n <= 1:
    	return n
	else:
    	return n + rsum(n-1)

# we are taking a number from user as input
# entered value will be converted to int from string
num = int(input("Enter a number: "))

#accepts a number as input from user and sends it as argument to rsum() function
ttl=rsum(num)
print("The sum is",ttl)

Let’s run the code and see how it works.

Output:

Enter a number: 15
The sum is 120

So, the sum of the first 15 natural numbers (1 + 2 + 3 + … + 15) is 120.

Recursion can be a powerful tool to solve problems in computer programming, and finding the sum of natural numbers is a classic example. Understanding how recursion works is an important aspect of computer programming and will help you in solving more complex problems in the future.

In this tutorial, we saw how to write a simple code to find the sum of natural numbers using recursion. You can use the same concept to find the sum of other sequences as well, for example, even numbers or odd numbers. You can also try to modify the code to find the sum of squares of natural numbers.

In conclusion, using recursion to find the sum of natural numbers is a simple yet effective way to learn the basics of recursion. Recursion can seem confusing at first, but with practice and a solid understanding of the concept, you’ll find that it can be a very useful tool for solving problems in computer programming.

Remember, the key to using recursion effectively is to make sure that you have a base case (the condition that will stop the recursion) and that the problem you’re trying to solve gets smaller with each recursive step. With these two things in place, you’ll be able to use recursion to solve a wide variety of problems.

So, keep practising and experimenting with recursion, and you’ll soon be able to use it to solve complex problems with ease. I hope this tutorial helped understand the concept of recursion and how to find the sum of natural numbers using it. If you have any questions, feel free to ask.

For More Python Programming Exercises and Solutions check out our Python Exercises and Solutions

About The Author

Leave a Reply