How to calculate a factorial of a number??

0 votes
asked Oct 11 by Cyn Niebla (120 points)
resultado=0
numero=int(input("introduce numero para factorial: "))

for i in range(0,(numero+1)):
    resultado=resultado+i*(i+1)

print(resultado)

2 Answers

0 votes
answered Oct 14 by Peter Minarik (9,990 points)

I think your main problem is that you didn't properly understand how the factorial is calculated.

Please, read it here.

So based on this, you could implement factorial with a for loop like this:

def factorial(n):
    product = 1
    for i in range (2, n + 1):
        product *= i
    return product

I'd suggest trying to implement it with recursion as well (the function calls itself, instead of a for loop).

+1 vote
answered Oct 16 by Harshitha (200 points)

It can also be done using recursion.

First of all, you better try to understand What is factorial?

Then it will be very easy for you  to get the correct answer.

def factorial(n):
    if n==1 or n==0:
        return 1
    else:
        return (n*factorial(n-1))
n=int(input("Enter any number: "))
print("Factorial of the given number",factorial(n))

Factorial is done using linear recursion.

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...