Follow Our FB Page << CircleMedia.in >> for Daily Laughter. We Post Funny, Viral, Comedy Videos, Memes, Vines...


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

Answers were Sorted based on User's Feedback



Evaluate the following: int fn(int v) { if(v==1 || v==0) return 1; if(v%2==0) return fn(v/2)+2..

Answer / 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 ?    12 Yes 1 No

Evaluate the following: int fn(int v) { if(v==1 || v==0) return 1; if(v%2==0) return fn(v/2)+2..

Answer / harish

11

Is This Answer Correct ?    10 Yes 2 No

Evaluate the following: int fn(int v) { if(v==1 || v==0) return 1; if(v%2==0) return fn(v/2)+2..

Answer / abdur rab

The answer is 11

( 7 - 1 ) + 3 -> 11 = (8 +3)

( 6 / 2 ) + 2 --> 8 = (6 +2)

( 3 - 1 ) + 3 ---> 6 = (3 +3)

( 2 / 2 ) + 2 ----> 3 = (1 +2)

Is This Answer Correct ?    5 Yes 1 No

Evaluate the following: int fn(int v) { if(v==1 || v==0) return 1; if(v%2==0) return fn(v/2)+2..

Answer / vinay

11 because it resrve every value in stack untill it become
1 then add all values.

Is This Answer Correct ?    3 Yes 0 No

Evaluate the following: int fn(int v) { if(v==1 || v==0) return 1; if(v%2==0) return fn(v/2)+2..

Answer / chandrakala

this program returns the result as

1.


because,
7 is not equal to 1 or 0 and it is not mod by 2. so it will
go to else condition .in that 7-1 is performed and it mod
by 2 so 6/2=3 it once again go to else 3-1=2 is enter in to
function 2%2\=0 so 2/2 is performed.now v is 1 so it
returns 1 as answer.

Is This Answer Correct ?    4 Yes 8 No

Evaluate the following: int fn(int v) { if(v==1 || v==0) return 1; if(v%2==0) return fn(v/2)+2..

Answer / 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



More C Interview Questions

Why & is used in scanf in c?

0 Answers  


do u print this format '(((())))'. This brackets is based on user input like 4 or 5 or 6,without using any loop's?

1 Answers   Oracle,


while running a program, i got the msg that press return key to exit.what that mean in C as there are no such options as far i know.

1 Answers   TCS,


Can we declare a function inside a function in c?

0 Answers  


Difference between C and Embedded C?

1 Answers  






Explain the difference between ++u and u++?

0 Answers  


What is the difference between macros and inline functions?

5 Answers   Global Edge, L&T,


9.how do you write a function that takes a variable number of arguments? What is the prototype of printf () function? 10.How do you access command-line arguments? 11.what does ‘#include<stdio.h>’ mean? 12.what is the difference between #include<> and #include”…”? 13.what are # pragma staments? 14.what is the most appropriate way to write a multi-statement macro?

4 Answers   L&T,


Write a program that will read the input of any number of digits n in a row of shafh showing the breakdown of the printing and printing figures by the recursive function.

0 Answers  


When c language was developed?

0 Answers  


Why is c so important?

0 Answers  


What are the primitive data types in c?

0 Answers  






Categories