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


Please Help Members By Posting Answers For Below Questions

Can you pass an entire structure to functions?

696


Why c is a procedural language?

584


Explain the bubble sort algorithm.

646


Can we compile a program without main() function?

634


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.

2455






Can we use any name in place of argv and argc as command line arguments?

610


Can you please explain the scope of static variables?

602


What is scope rule in c?

606


What are external variables in c?

546


What are valid operations on pointers?

669


How does normalization of huge pointer works?

641


Why do we use header files in c?

584


List the difference between a While & Do While loops?

639


What are the types of bitwise operator?

665


What is gets() function?

673