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 / anju nair

the answer is : 11

solution :

since v=7

initially f(6)+ 3 is executed ( being a recurrsive
function) fn is again called with v=6 and the return now
is f(3) + 2 and again fn is called with v=3 and the return
now is f(2) +3 and again fn is called with v=0.now
recurrsion ends since f(0) is 1

f(0)=1
f(0)+2=3

f(2)=f(0)+2=1+2
f(2)+3=6

f(3)=f(2)+3=6
f(3)+2=6+2=8

f(6)=f(3)+2=8
f(6)+3=8+3=11

f(7)=f(6)+3
f(7)=11

now v hav to back track and keep adding
1+2+3+2+3 = 11.

Is This Answer Correct ?    14 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is the difference between a function and a method in c?

560


Differentiate between declaring a variable and defining a variable?

608


What is far pointer in c?

813


Why pointers are used?

633


What are called c variables?

574






write a c program in such a way that if we enter the today date the output should be next day's date.

1681


What is a function simple definition?

618


What are pointers really good for, anyway?

617


Why main is not a keyword in c?

650


What is the use of volatile?

610


a function gets called when the function name is followed by a a) semicolon (;) b) period(.) c) ! d) none of the above

874


Tell us the use of fflush() function in c language?

639


which of the following shows the correct hierarchy of arithmetic operations in C a) (), **, * or/,+ or - b) (),**,*,/,+,- c) (),**,/,*,+,- d) (),/ or *,- or +

1186


Explain the process of converting a Tree into a Binary Tree.

2106


Write a program to check whether a number is prime or not using c?

576