Python Program to Find the Sum of Natural Numbers using Recursion
In this tutorial, you will learn how to Find the Sum of Natural Numbers using Recursion using the if, else statement with the different operators of the python programming language.
Recursion is the process of defining something in terms of itself. In the code below we’ve used a recursive function to compute the sum up to the given number.
How to Find the Sum of Natural Numbers using Recursion?
Let’s take a look at the source code , here the values are given as input by the user in the code, the if statement and for loop along with the assignment operators carry out the function.
# 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) num = int(input("Enter the integer: ")) if num < 0: print("Enter a positive number") else: print("\nThe sum is",recur_sum(num))
Enter the integer: The sum is 78
- At the start, we use
def recur_sum(n):where the
defkeyword is used to define a function and the
recur_sumis used to call the function to get the value of the variable
- We declare an
ifstatement with the condition
n <= 1where if it is satisfied, using the
returnfunction we return the value of n, if the condition is not satisfied, it moves to the next step which is the
elsestatement where the value obtained after the execution of the equation
n + recur_sum(n-1)is returned using the
- Here we give the user the option to enter the values and the input values are scanned using the
inputfunction and are stored in the variable
numwith the statements/strings
("Enter the integer: "), we use the
intfunction and declare the input value as an integer value.
- In the STDIN section of the code editor the input values are entered.
- We declare another
ifstatement with the condition
num < 0where if it is satisfied, the
("Enter a positive number"), if the condition is not satisfied, it moves to the next step which is the
elsestatement, where the
("\nThe sum is",recur_sum(num), where the function
recur_sum(num)holds the final value of the recursive function will be displayed.
- Recursive functions are functions that calls itself. It is always made up of 2 portions, the base case and the recursive case. The base case is the condition to stop the recursion. The recursive case is the part where the function calls on itself.
- The < lesser than operator is a comparison operator which returns True if the condition is satisfied, if not it returns false.
- The <= lesser than and equal to operator is a comparison operator which returns True if the condition is satisfied, if not it returns false.
- The input() function allows a user to insert a value into a program, it returns a string value.
- The if and else statements evaluates whether an expression is true or false. If a condition is true, the “if” statement is executed otherwise, the “else” statement is executed.
- The colon : at the end of the if and else statement tells Python that the next line of code should only be run if the condition is true.
- The statement for the input function are enclosed in single quotes and parenthesis.
- The \n in the code indicates a new line or the end of a statement line or a string.
- The print statement/string to be displayed in enclosed in double quotes.