Gate
Exam
Programming
Database
Consider the following C function:
int f(int n)
{
static int are = 0;
If (n < = 0) return 1;
If (n > 3)
{ r = n;
return f (n – 2) + 2;
}
return f(n – 1) + r;
}
What is the value of f(5)?
Read Solution (Total 4)
-
- r variable is not declare.
while compiling time error will show. - 10 years agoHelpfull: Yes(2) No(1)
- its answer would be either 5 or error as n is not properly defined.
- 10 years agoHelpfull: Yes(0) No(1)
- error is thrown as variable r is not declared.
- 10 years agoHelpfull: Yes(0) No(0)
- first correct the question .....its int r=0; ...not int are=0;
Ans = 18
when u first call f(5) the 5>3 so r=5 and then call f(3)+2=16+2=18
then call f(2)+5=11+5=16
f(1)+5=6+5=11
f(0)+5=1+5=6
f(0)=1 - 8 years agoHelpfull: Yes(0) No(0)
Gate Other Question