📅  最后修改于: 2023-12-03 15:28:43.701000             🧑  作者: Mango
Consider the C code fragment given below:
void f(int n)
{
if(n<=0) return;
printf("%d",n);
f(n-2);
printf("%d",n);
}
What is the output of the following function for input 4?
void main()
{
f(4);
}
A. 41324
B. 43124
C. 41342
D. 43142
The function f(int n)
prints the integers in the following order for an input of n
:
n
is less than or equal to 0
, return from the function.n
.f(n-2)
.n
.So, if the input is 4, then the order of printing would be:
4
- Step 22
- Step 2 (from f(2)
- Step 3)2
- Step 4 (from f(2)
- Step 3)4
- Step 4 (from f(2)
- Step 3)1
- Step 2 (from f(1)
- Step 3)1
- Step 4 (from f(1)
- Step 3)3
- Step 4 (from f(3)
- Step 3)3
- Step 2 (from f(3)
- Step 3)4
- Step 44
- Step 2Therefore, the output would be 2412434
.
Hence, the correct answer is option A.
void f(int n)
{
if(n<=0) return;
printf("%d",n);
f(n-2);
printf("%d",n);
}
void main()
{
f(4);
}