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 Display Calendar
Python String format_map()
Python Set intersection()
Python Set discard()
Python String isalnum()
Python *args and **kwargs
Python Set difference_update()
Python super()
Python time Module
Python Program to Remove Duplicate Element From a List
Python property()
Python Program to Count the Occurrence of an Item in a List
Python Shallow Copy and Deep Copy
Python String casefold()
Python String isalpha()
Python set()
Python vars()
Python List clear()
Python Program to Check if a Number is Positive, Negative or 0
Python Dictionary items()
Python locals()
Python Strings
Python Program to Illustrate Different Set Operations
Python Set issubset()
Python str()
Deep Learning with Python - Francois Chollet
Python Get Current time
Python Program to Get the Full Path of the Current Working Directory
Python Deeper Insights into Machine Learning - Sebastian Raschka & David Julian & John Hearty
Python Set add()
Python Set intersection_update()
Python List Comprehension