Question 2:

Write a program which can compute the factorial of a given number. The results should be printed in a comma-separated sequence on a single line.

Suppose the following input is supplied to the program: 8 Then, the output should be: 40320

For example(5 factorial),

5 ! = 5 × 4 × 3 × 2 × 1 = 120 .

For example(4 factorial),

4 ! = 4 × 3 × 2 × 1 = 24 .

Hints:

1. In case of input data being supplied to the question, it should be assumed to be a console input.

2. Write a function and pass the factorial value as input

3. Subtract value by 1 and multiply it original value

4. Repeat the step 3 until the value reaches zero

To know more about factorial

https://en.wikipedia.org/wiki/Factorial

Solution:

def fact(x):
    if x == 0:
        return 1
    return x * fact(x - 1)

x=int(input())
print(fact(x))

 

Discussion:

1. Write a function to call it recursively until it reaches zero value

2. Get input from user using the input() function and convert it to interger using int() function

3. Call fact() function and pass the value and print the return result using print() function