In this program, you’ll learn to find the sum of natural numbers using recursive function.
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.
Related posts:
Python open()
Python Program to Find the Square Root
Python iter()
Python String rindex()
Python Program to Measure the Elapsed Time in Python
Python Program to Reverse a Number
Python complex()
Python Set difference()
Python oct()
Python List pop()
Python Program to Find Numbers Divisible by Another Number
Python property()
Python Decorators
Python eval()
Python List index()
Python String expandtabs()
Java Program to Delete a Particular Node in a Tree Without Using Recursion
Deep Learning with Python - Francois Chollet
Python Set symmetric_difference_update()
Python Program to Count the Occurrence of an Item in a List
Python Program to Check If Two Strings are Anagram
Python float()
Python Program to Find the Factors of a Number
Python Dictionary clear()
Python Program to Convert Two Lists Into a Dictionary
Python Keywords and Identifiers
Python pass statement
Python List reverse()
Machine Learning Applications Using Python - Cases studies form Healthcare, Retail, and Finance - Pu...
Python ascii()
Python Program to Check Leap Year
Python String isnumeric()