Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Factorial program in python

  • The term "Factorial" means multiplication of all the integers the given number as we want the fact of it.
  • Factorial is applicable for only positive integers, for 0! it is always 0.
  • Simply if we want the factorial of 3 then it is written as  3!=3*2*1
  • The formula is Fact=n*(n-1)*(n-2)*.......1

#write a python program for factorial of a given number.

num = int(input("Enter a number: "))
factorial = 1
if num
   print("factorial 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)

  • We store the number we give in the num variable and by using the if, elif, and else loop we are executing the program. if statement executes when the given condition is true and same for elif also. After the iteration, it finally returns the factorial of the number we are given.
  • Normally we write as 12!=12*11*10*9*8*7*6*5*4*3*2*1=479001600






This post first appeared on Java Tutorial - InstanceOfJava, please read the originial post: here

Share the post

Factorial program in python

×

Subscribe to Java Tutorial - Instanceofjava

Get updates delivered right to your inbox!

Thank you for your subscription

×