KnowledgeBoat Logo
|

Robotics & Artificial Intelligence

Write a user-defined function def Fact() to input 1 to 5 from the user. The program calculates the factorial of each number inside the function. Finally, it returns the values to main program to display the result.

Hint:

def Fact(n):
    f = 1
    for a in range(1, n+1):
        f = f * a
    return f
# main program
for i in range(1, 6):
    print("Factorial of", i, "=", Fact(i))

Python Functions

1 Like

Answer

def Fact(n):
    f = 1
    for a in range(1, n + 1):
        f = f * a

    return f

# main program
for i in range(1, 6):
    print("Factorial of", i, "=", Fact(i))

Output

Factorial of 1 = 1
Factorial of 2 = 2
Factorial of 3 = 6
Factorial of 4 = 24
Factorial of 5 = 120

Answered By

3 Likes


Related Questions