Factorial of a Number program in Python
So we need to know that what is Factorial of a Number.
Suppose, 5 is a Number. so, to get the factorial of an number, we are multiplies all those numbers which's comes before those number like, 5 x 4 x 3 x 2 x 1
5 : 5 x 4 x 3 x 2 x 1 = 120
Ex,
Given example shows the actual meaning of factorial of a Number.
Starting of Program in Python :
Firstly we need to take a number from user. for that we used,
num = int(input("Please enter a Number : "))
we created a variable with name 'num' and in that we stored those number which is entered by user.
To take the input from user, we used input() function.
After that, we created a variable 'result' to store the result of our Factorial.
result = 1
After that, I have used a for-loop to iterate a statement or a block of code to multiply the all numbers.
for i in range(1,num+1):
result = result * i
In this for-loop, i declared a variable 'i' to iterate a the value of 'i' in a range, which helps to gather all the numbers comes before those number which is entered by user.
By using this for-loop we applied our logic in this program.
print("")
print(f"The factorial of {num} is {result}")
print("")
Here we just displayed our Factorial of those Number...!
Output :
Please enter a Number : 5
The factorial of 5 is 120
What do you learn :
1) How to create a Python File
2) How to create Variables in Python
3) How to use input function in Python
4) How to take the input from the User
5) How to typecast the value of a variable
6) How to use f-string in Python
7) How to create Empty String
8) How to use For-Loop in Python
9) How to use 'in range() function' in Python
10) How to increament the value of any Variable
0 Comments
Please do not add Any Spam link in Comments !