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

  1. 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
  2. num = int(input("Enter a Number: "))
  3. 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
  4. if (num == 0):
      fact = 1
  5. 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
  6. fact = 1
    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

Comments

Post a Comment

Popular posts from this blog

String Encoding and Decoding in Python

Python secrets module - Token Generator