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

what is ur strangth & weekness

1836


Is Exception handling possible in c language?

1590


Stimulate calculator using Switch-case-default statement for two numbers

2458


What does it mean when the linker says that _end is undefined?

648


What is mean by Data Driven framework in QTP? Can any one answer me in details on this regard.

1790






How can I get back to the interactive keyboard if stdin is redirected?

677


Do pointers need to be initialized?

575


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

672


What is #line in c?

572


Write a program to reverse a linked list in c.

655


What are local static variables?

629


What are the types of c language?

566


If fflush wont work, what can I use to flush input?

629


What is c method?

546


How we can insert comments in a c program?

638