Find Factorial:Python program to using function
- Home
- Find elements
- Find Factorial:Python program to using function
- On
- By
- 4 Comments
- Categories: Find elements
Find Factorial:Python program to using function
Python program to find factorial using function
In this tutorial, we will discuss the Python program to find factorial using function
In this program, we are going to learn about how to find factorial using the function in Python language
Factorial is a product of all positive descending integer begins with a specified number (n) and calculates up to one
- Factorial is not defined for negative numbers
- Factorial of zero(0) is: 1
- Factorial of a positive number is given below
Example
factorial of 5 is
5!=5*4*3*2*1=120
factorial of 7 is
7!=7*6*5*4*3*2*1=5040
The factorial of a positive number n is given below
factorial of n is
n!=n*(n-1)*....2*1
In my previous post, I have explained the various ways to Find factorial of a number in Python
However, in this program, we embed the logic of the factorial program in the function
you can embed the logic of the factorial program to find the factorial of numbers using any of the following approaches in the function
The user can provide numbers as they wish and get the factorial according to their input
Program 1
Find factorial using Python function – using for loop
#Pyton program to find factorial of a number def factorial(num):#function definition fact=1 for i in range(1, num+1):#for loop for finding factorial fact=fact*i return fact #return factorial number=int(input("Please enter any number to find factorial: ")) result=factorial(number)#function call and assign the value to variable result print("The factorial of %d = %d"%(number,result))
When the above code is executed, it produces the following results
Please enter any number to find factorial: 6 The factorial of 6 = 720
Find factorial using Python function – using while loop
def factorial(num):#function definition fact=1; i=1 while i<=num: fact=fact*i i=i+1 return fact num=input("Enter a number.."); result=factorial(num) print("factorial of the number: %d" %result) factorial(num)#function call
When the above code is executed, it produces the following results
Enter a number..5 factorial of the number: 120
Programming approaches for implementing these program
- Take a number as input from the user and stored in variable number for finding factorial
- Declare a python function to find factorial
- Inside the function, declare the variable fact and initialise as 1
- Inside the function, find the factorial of a given number using for loop in Python
- Run the loop from given number until 1 and multiply numbers
- Call the factorial() function and assign the output to variable result
- the factorial of the given number is displayed using the print() function in Python
Check out these related examples
Java code to calculate the factorial of a number
Java code to calculate the factorial of a number using Java method
Calculate factorial of a number in C
Calculate factorial of a number in CPP
Calculate factorial of a number in Python
Calculate factorial of a number using while loop in Python
Suggested for you
4 Comments
fleck water softener installation April 5, 2020 at 10:32 am
Fantastic site you have here but I was curious about if you knew of any user discussion forums that cover the same topics talked about here?
I’d really like to be a part of online community where I can get suggestions from other knowledgeable individuals that share the same interest.
If you have any suggestions, please let me know. Thanks!
Karmehavannan April 6, 2020 at 6:08 am
ok ,sure
Patricia Bentley April 16, 2020 at 2:47 am
I think you should make a video about this on YouTube…
Karmehavannan April 17, 2020 at 12:57 am
Sure I try to it