Python Program to Find The Factorial of an Input Number

Python Program to Find The Factorial of an Input Number: In this program, we will learn to write a code to check the factorial of an input number.


Python Program to Find The Factorial of an Input Number


In this program we use:
  • if-else statement
  • for loop

Working:
  • Factorial is the product of integer from 1 to the given number.
  • Factorial is not defined for negative integers.
  • Factorial of zero is always 1 (0!=1).

Source Code:
print("           Scribbled Writer.")

num = int(input("Enter a number: "))

factorial = 1

# check if the number is negative, positive or zero
if num < 0:
   print("Sorry, factorial does not exist for negative numbers.")
elif num == 0:
   print("The factorial of 0 is 1.")
else:
   for i in range(1,num + 1):
       factorial = factorial*i
   print("The factorial of",num,"is",factorial)

print("            Happy Coding!")   



Output:

Ex-1:
           Scribbled Writer.
Enter a number: 17
The factorial of 17 is 355687428096000
            Happy Coding!

Ex-2:
           Scribbled Writer.
Enter a number: -3
Sorry, factorial does not exist for negative numbers.
            Happy Coding!

Ex-3:
           Scribbled Writer.
Enter a number: 0
The factorial of 0 is 1
            Happy Coding!


Liked the content, share with your fellow mates.
Any feedback feel free to drop in the comment box.

1 comment: