Computer Science

Fill in the line of code for calculating the factorial of a number:

def fact(num):
    if num == 0:
        return 1
    else:
        return...............
  1. num*fact (num-1)
  2. (num-1) * (num-2)
  3. num* (num-1)
  4. fact (num) *fact (num-1)

Python Functions

1 Like

Answer

num*fact (num-1)

Reason — The fact function is designed to calculate the factorial of a given number through recursion. It checks if the input num is zero; if so, it returns 1 as the factorial of 0 is defined as 1. Otherwise, it recursively calls itself with the argument num-1, reducing num with each call until reaching 0, which serves as the base case to stop recursion. During each recursive call, the function multiplies the current num by the factorial of num-1, effectively computing the factorial of the original input number through successive multiplications until the base case is reached.

Answered By

1 Like


Related Questions