Navigation X
ALERT
Click here to register with a few steps and explore all our cool stuff we have to offer!



 2801

Python How to Find the Factorial of a Number

by Black_Kais3r - 12 April, 2019 - 12:01 AM
This post is by a banned member (Black_Kais3r) - Unhide
68
Posts
38
Threads
6 Years of service
#1
Code:
# Python program to find the factorial of a number provided by the user.

# change the value for a different result
num = 7

# uncomment to take input from the user
#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)
This post is by a banned member (bowtodylan) - Unhide
3
Posts
0
Threads
5 Years of service
#2
# way 1

def factorial(num):
   if num == 1:
       return 1
   else:
       return num * factorial(num - 1)
print(factorial(n))

# way 2

fac = 1
for i in range(1,n + 1):
   fac *= i


print(fac)

I'll add more soon XD ,these were the only ones i conjure up in 3 minutes
This post is by a banned member (hasi912) - Unhide

Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
or
Sign in
Already have an account? Sign in here.


Forum Jump:


Users browsing this thread: 1 Guest(s)