Python Program to Find Sum of Natural Numbers Using Recursion

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


In the program below, we've used a recursive function recur_sum() to compute the sum up to the given number.

Source Code

# Python program to find the sum of natural using recursive function

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

# change this value for a different result
num = 16

if num < 0:
   print("Enter a positive number")
else:
   print("The sum is",recur_sum(num))

Output

The sum is 136

Note: To test the program for another number, change the value of num.


Also Read:

Did you find this article helpful?

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges