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 Swap Two Variables
Python Program to Find Factorial of Number Using Recursion
Python timestamp to datetime and vice-versa
Python Program to Check Whether a String is Palindrome or Not
Python Program to Convert Kilometers to Miles
Python Set symmetric_difference()
Python Program to Sort Words in Alphabetic Order
Python Program to Differentiate Between type() and isinstance()
Python Program to Remove Punctuations From a String
Python List
Python Program to Display Calendar
Python __import__()
Python Program to Iterate Over Dictionaries Using for Loop
Python Program to Check Prime Number
Building Chatbots with Python Using Natural Language Processing and Machine Learning - Sumit Raj
Python Program to Get a Substring of a String
Python Program to Count the Occurrence of an Item in a List
Python int()
Python Get Current time
Python Program to Check Leap Year
Python Program to Find the Largest Among Three Numbers
Python Program to Print Hello world!
Python Dictionary pop()
Python Dictionary get()
Machine Learning with Python for everyone - Mark E.Fenner
Python Program to Get the Last Element of the List
Python frozenset()
Python Program to Make a Simple Calculator
How to get current date and time in Python?
Python Program to Parse a String to a Float or Int
Python property()
Python Program to Display Fibonacci Sequence Using Recursion