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
Can you pass an entire structure to functions?
Why c is a procedural language?
Explain the bubble sort algorithm.
Can we compile a program without main() function?
write a C program: To search a file any word which starts with ?a?. If the word following this ?a? starts with a vowel.Then replace this ?a? with ?a? with ?an?. redirect with the output onto an output file.The source file and destination file are specified by the user int the command line.
Can we use any name in place of argv and argc as command line arguments?
Can you please explain the scope of static variables?
What is scope rule in c?
What are external variables in c?
What are valid operations on pointers?
How does normalization of huge pointer works?
Why do we use header files in c?
List the difference between a While & Do While loops?
What are the types of bitwise operator?
What is gets() function?