Factorial Program in python
Code
num = int(input("Enter a Number: "))
if (num == 0):
fact = 1
fact = 1
for i in range(1, num5+1):
fact = fact * i
print("Factorial of ", num, " is ", fact)
Explaination
- getting the input using the input function, But the input must be in the input format so for getting the input as integer type use int() function
- Check weather the given number is zero or not if the number is zero then the factorial value will be 1, so we can assume that fact is 1
- if the entered value is not equal to one then assume one variable fact = 1 and then create a for loop which starts from one to the value we want to find the fact value, And then multiple the previous fact value with the loop value
For example:
lets assume the loop value is [1,2,3,4.....n] then it will multiple the fact value with i value and stores the new fact value
num = int(input("Enter a Number: "))
if (num == 0):
fact = 1
fact = 1
fact = 1
for i in range(1, num5+1):
fact = fact * i
print("Factorial of ", num, " is ", fact)
for i in range(1, num5+1):
fact = fact * i
print("Factorial of ", num, " is ", fact)
Output:
Enter a Number: 12
Factorial of 12 is 479001600
Enter a Number: 12
Factorial of 12 is 479001600
This comment has been removed by the author.
ReplyDelete