In this tutorial, we discuss Python language Recursive function.
When a function calls itself it is called as recursion. The python language allows creating the recursive function to perform a special task
The following example explains how to find the adding of the limited natural numbers using the recursive function
The following is an example of the recursive function to total a series of the integer numbers
Program 1
def sum_Num(num): if num == 0: return 0 else: return num + sum_Num(num-1) print("Sum of the first 100 numbers is",sum_Num(100)) print("Sum of first 500 numbers is",sum_Num(500))
When the above code executed, it produces the following results
Sum of the first 100 numbers is 5050 Sum of first 500 numbers is 125250
In the above example, to find the total of given numbers, when we passing the argument of the positive integer of this function, it will recursively call itself decreasing one by one until it becomes one from a given number.
Program 2
following example explain how to find the factorial of a number using a recursive function in python.
factorial of a number is the product of the all integer from one to until the given number
For example, the factorial of 5 is 1*2*3*4*5n=120
#This is a example for find factorial #using recursive function in Pyton def calc_fact(n): if n==1: return 1; else: return(n*calc_fact(n-1)) num= int(input("Enter the number for find factorial: ")) # take input from user for find factorial print("The factorial of ",num," is", calc_fact(num)) #display factorial
When the above code executed, it produces the following results
Enter the number for find factorial: 5 The factorial of 5 is 120
In the above example, calc_fact() is a recursive function as it calls itself to find factorial.
Factorial of n is the product of all the integer from 1 to the factorial of n is 1*2*3*4……..*n-1*n.
In the above example, to find the factorial of a given number, when we pass the argument as the positive integer of this function, it will recursively call itself decreasing one by one until it becomes one from a given number
Suggested for you
Find factorial using recursion in Python
Find factorial using recursion in C language
Find factorial using recursion in C++ language
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…
Java program to check odd or even using recursion In this tutorial, we discuss a…