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 zip()
Python Program to Create a Long Multiline String
Python Program to Iterate Over Dictionaries Using for Loop
Python Set update()
Python Function Arguments
Python dir()
Python List Comprehension
Python String swapcase()
Intelligent Projects Using Python - Santanu Pattanayak
Python Program to Get a Substring of a String
Python Recursion
Python Matrices and NumPy Arrays
Python Deeper Insights into Machine Learning - Sebastian Raschka & David Julian & John Hearty
Python String isspace()
Python globals()
Python list()
Machine Learning Mastery with Python - Understand your data, create accurate models and work project...
Python String isnumeric()
Python Program to Check If a String Is a Number (Float)
Python Program to Check Armstrong Number
Python Program to Extract Extension From the File Name
Python Program to Illustrate Different Set Operations
Python Set intersection()
Python String split()
Python abs()
Building Chatbots with Python Using Natural Language Processing and Machine Learning - Sumit Raj
Python ord()
Python String strip()
Python for Programmers with introductory AI case studies - Paul Deitel & Harvey Deitel
Python Program to Convert Celsius To Fahrenheit
Python Program to Concatenate Two Lists
Python Exception Handling Using try, except and finally statement