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...............
- num*fact (num-1)
- (num-1) * (num-2)
- num* (num-1)
- 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
2 Likes
Related Questions
What is the output of the program given below?
x = 50 def func(x): x = 2 func(x) print('x is now', x)- x is now 50
- x is now 2
- x is now 100
- Error
Which is the most appropriate definition for recursion?
- A function that calls itself
- A function execution instance that calls another execution instance of the same function
- A class method that calls another class method
- An inbuilt method that is automatically called
Which of the following statements is false about recursion?
- Every recursive function must have a base case.
- Infinite recursion can occur if the base case isn't properly mentioned.
- A recursive function makes the code easier to understand.
- Every recursive function must have a return value.
What is the output of the following snippet?
def fun(n): if (n > 100): return n - 5 return fun (fun (n+11) ) print(fun(45))- 50
- 100
- 74
- Infinite loop