Consider the following recursive implementation to find the factorial of a number. Which of the lines should be inserted to complete the

Tiên Hương

New member
Consider the following recursive implementation to find the factorial of a number. Which of the lines should be inserted to complete the below code?
Code:
int fact(int n)
{
     if(_________)
        return 1;
     return n * fact(n - 1);
}
int main()
{
      int n = 5;
      int ans = fact(n);
      printf("%d",ans);
      return 0;
}
A. n = 0
B. n != 0
C. n == 0
D. n == 1
 
Answer: C
Explanation: The line "n == 0" should be inserted to complete the above code.
Note: "n == 1" cannot be used because it does not take care of the case when n = 0, i.e when we want to find the factorial of 0.
 

Tìm kiếm

Back
Top