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 a function simple definition?
What is the stack in c?
What are the advantages of using macro in c language?
Tell me when would you use a pointer to a function?
What is wrong with this initialization?
Can a pointer be volatile in c?
Explain the advantages of using macro in c language?
What is the difference between fread and fwrite function?
What is the use of header?
What are the primitive data types in c?
What are the different types of objects used in c?
What is string function in c?
What is the difference between struct and union in C?
Who developed c language?
What is the ANSI C Standard?