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 Program to Get File Creation and Modification Date
Python Program to Find ASCII Value of Character
Python int()
Python Set difference_update()
Python Program to Swap Two Variables
Python Program to Find LCM
Python String startswith()
Python issubclass()
Python Set clear()
Python Data Analytics with Pandas, NumPy and Matplotlib - Fabio Nelli
Python Program to Extract Extension From the File Name
Python id()
Python Program to Create a Long Multiline String
Python String isalnum()
Python Program to Check If Two Strings are Anagram
Python Program to Split a List Into Evenly Sized Chunks
Python repr()
Python tuple()
Python RegEx
Python Type Conversion and Type Casting
Python 3 for Absolute Beginners - Tim Hall & J.P Stacey
Python isinstance()
Python Program to Check Armstrong Number
Python Inheritance
Python Program to Convert Celsius To Fahrenheit
Python Program to Slice Lists
Python getattr()
Python Program to Convert Two Lists Into a Dictionary
Python round()
Building Machine Learning Systems with Python - Willi Richert & Luis Pedro Coelho
Python String isidentifier()
Python Closures