Programa Python para factorial de un número

Factorial de un entero no negativo, es la multiplicación de todos los enteros menores o iguales a n. Por ejemplo, el factorial de 6 es 6*5*4*3*2*1, que es 720.

factorial

python3

# Python 3 program to find
# factorial of given number
def factorial(n):
     
    # single line to find factorial
    return 1 if (n==1 or n==0) else n * factorial(n - 1);
 
# Driver Code
num = 5;
print("Factorial of",num,"is",
factorial(num))
 
# This code is contributed by Smitha Dinesh Semwal

python3

# Python 3 program to find 
# factorial of given number
def factorial(n):
    if n < 0:
        return 0
    elif n == 0 or n == 1:
        return 1
    else:
        fact = 1
        while(n > 1):
            fact *= n
            n -= 1
        return fact
 
# Driver Code
num = 5;
print("Factorial of",num,"is",
factorial(num))
 
# This code is contributed by Dharmik Thakkar

Python3

# Python 3 program to find
# factorial of given number
 
def factorial(n):
 
    # single line to find factorial
    return 1 if (n==1 or n==0) else n * factorial(n - 1)
 
 
# Driver Code
num = 5
print ("Factorial of",num,"is",
      factorial(num))
 
# This code is contributed
# by Smitha Dinesh Semwal.

Python3

# Python 3 program to find
# factorial of given number
import math
 
def factorial(n):
    return(math.factorial(n))
 
 
# Driver Code
num = 5
print("Factorial of", num, "is",
      factorial(num))
 
# This code is contributed by Ashutosh Pandit

Publicación traducida automáticamente

Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *