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
What are linked lists in c?
What are pointers in C? Give an example where to illustrate their significance.
What is ambagious result in C? explain with an example.
printf(), scanf() these are a) library functions b) userdefined functions c) system functions d) they are not functions
main() { printf("hello"); fork(); }
What is the significance of an algorithm to C programming?
What is header file definition?
Write a factorial program using C.
What is scanf_s in c?
Is c call by value?
Write a program to reverse a linked list in c.
Why is c called a structured programming language?
Explain how can type-insensitive macros be created?
hw can we delete an internal node of binary search tree the internal node has child node..plz write progarm
What is the difference between typedef struct and struct?