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
what is ur strangth & weekness
Is Exception handling possible in c language?
Stimulate calculator using Switch-case-default statement for two numbers
What does it mean when the linker says that _end is undefined?
What is mean by Data Driven framework in QTP? Can any one answer me in details on this regard.
How can I get back to the interactive keyboard if stdin is redirected?
Do pointers need to be initialized?
any restrictions have on the number of 'return' statements that may be present in a function. a) no restriction b) only 2 return statements c) only 1 return statements d) none of the above
What is #line in c?
Write a program to reverse a linked list in c.
What are local static variables?
What are the types of c language?
If fflush wont work, what can I use to flush input?
What is c method?
How we can insert comments in a c program?