fastread homefastrread library fastread menu

Python : Find Sum of Natural Numbers Using Recursion

Tutorial by:Rizwan Abdullah      Date: 2016-06-16 01:45:04

❰ Previous Next ❱

Source Code

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

def recur_sum(n):
   """Function to return the sum
   of natural numbers using recursion"""
   if n <= 1:
       return n
       return n + recur_sum(n-1)

# take input from the user
num = int(input("Enter a number: "))

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


Enter a number: 16
The sum is 136

In this program, we ask the user for a number and use recursive function recur_sum() to compute the sum up to that number. 

❰ Previous Next ❱


Submit Your Thought, Tutorial, Articls etc.

Submit Your Information India's Number one online promotion website