Last Updated :
18 Dec, 2018
What will be the output of the C program given below.
#include
int cal(int n, int *p) {
int t, f;
if (n <= 2) {
*p = 1;
return 1;
}
t = cal(n-1, p);
f = t + *p;
*p = t;
return f;
}
int main() {
int x = 6;
printf(\"%d\", cal(x, &x));
return 0;
}
[/sourcecode]
(A) 4
(B) 6
(C) 7
(D) 8
Answer: (D)
Explanation: x=6 and let &x=1000
cal(6,1000)
t=5, f=5 +3=8
*p=t=5
Return 8
cal(5,1000)
t=3, f=3 + 2=5
*p=t=3
Return 5
cal(4,1000)
t=2, f=2+1=3
*p=t=2
Return 3
cal(3,1000)
t=1,f=1+1=2
*p=t=1
Return 2
cal(2,1000)
*p=1
Return 1
So, option (D) is correct.
Quiz of this Question
Share your thoughts in the comments
Please Login to comment...