
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How to Find Sum of Natural Numbers Using Recursion in Python?
If a function calls itself, it is called a recursive function. In order to prevent it from falling in infinite loop, recursive call is place in a conditional statement.
Following program accepts a number as input from user and sends it as argument to rsum() function. It recursively calls itself by decrementing the argument each time till it reaches 1.
def rsum(n): if n <= 1: return n else: return n + rsum(n-1) num = int(input("Enter a number: ")) ttl=rsum(num) print("The sum is",ttl)
Sample run of above program prints sum o natural numbers upto input number
Enter a number: 10 The sum is 55
- Related Articles
- C++ program to Find Sum of Natural Numbers using Recursion
- Java Program to Find the Sum of Natural Numbers using Recursion
- Golang Program to Find the Sum of Natural Numbers using Recursion
- How to Find the Sum of Natural Numbers using Python?
- Java Program to Find Sum of N Numbers Using Recursion
- Golang Program to Find the Sum of N Numbers using Recursion
- Python Program to Find the Product of two Numbers Using Recursion
- How to Find Factorial of Number Using Recursion in Python?
- How to find the product of 2 numbers using recursion in C#?
- Python Program to Find the Total Sum of a Nested List Using Recursion
- Program to find sum of first n natural numbers in C++
- Java program to find the sum of n natural numbers
- How to find the LCM of two given numbers using Recursion in Golang?
- How to find the GCD of Two given numbers using Recursion in Golang?
- How to Calculate the Sum of Natural Numbers in Golang?

Advertisements