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 repr()
Python Program to Convert Two Lists Into a Dictionary
Python Program to Add Two Matrices
Machine Learning Applications Using Python - Cases studies form Healthcare, Retail, and Finance - Pu...
Python super()
Python Program to Concatenate Two Lists
Python Global, Local and Nonlocal variables
Python Program to Print all Prime Numbers in an Interval
Python Directory and Files Management
Python bytearray()
Python Program to Convert Decimal to Binary, Octal and Hexadecimal
Python Program to Copy a File
Python Program to Convert Celsius To Fahrenheit
Python Dictionary fromkeys()
Statistical Methods for Machine Learning - Disconver how to Transform data into Knowledge with Pytho...
Python String join()
Python for Programmers with introductory AI case studies - Paul Deitel & Harvey Deitel
Python Program to Find Hash of File
Python Set issubset()
Python Program to Safely Create a Nested Directory
Python break and continue
Python Set symmetric_difference()
Python Program to Reverse a Number
Python String find()
Python Program to Check if a Number is Positive, Negative or 0
JavaScript Recursion and stack
Python round()
Python Set intersection()
Python locals()
Python List clear()
Python Program to Solve Quadratic Equation
Python str()