YouTip LogoYouTip

Python3 Factorial

# Python3.x Python Factorial Example [![Image 3: Document Object Reference](#) Python3 Examples](#) The factorial of an integer (English: factorial) is the product of all positive integers less than or equal to that number. The factorial of 0 is 1. That is: n! = 1 Γ— 2 Γ— 3 Γ— ... Γ— n. ## Example #!/usr/bin/python3# Filename : test.py# author by : www..com# Calculate factorial based on user input# Get the number entered by the user num = int(input("Please enter a number: "))factorial = 1# Check if the number is negative, zero, or positive if num<0: print("Sorry, negative numbers do not have a factorial")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 %d is %d" %(num,factorial)) Executing the above code produces the following output: Please enter a number: 3 The factorial of 3 is 6 [![Image 4: Document Object Reference](#) Python3 Examples](#)
← Python3 File IoPython3 Largest Number β†’