Evaluate the following:
int fn(int v)
{
if(v==1 || v==0)
return 1;
if(v%2==0)
return fn(v/2)+2;
else
return fn(v-1)+3;
}
for fn(7);
1) 10
2) 11
3) 1
Answer Posted / lakshmi
first it checkes the main function i.e., fn()
int fn(7)
if(7==1||v==0) : this is false so the complier executes
the next if condition
if(7%2==0) : This is false so it executes the else
instruction
return fn(6)+3; :here we are again calling the fn
function .So the loop executes till the 7 becomes 1
After this the first if condition is true so it rerurns 1
to the function fn(1)+3
so the answer is 1.
| Is This Answer Correct ? | 0 Yes | 7 No |
Post New Answer View All Answers
Explain what does the format %10.2 mean when included in a printf statement?
What is a nested loop?
How can I read a binary data file properly?
What is modeling?
How does selection sort work in c?
Write a program for finding factorial of a number.
What is union and structure?
Explain what is the difference between functions abs() and fabs()?
What is meant by recursion?
Which header file is used for clrscr?
How can you be sure that a program follows the ANSI C standard?
What is function prototype?
the real constant in c can be expressed in which of the following forms a) fractional form only b) exponential form only c) ascii form only d) both a and b
Is there a way to compare two structure variables?
Which is best book for data structures in c?