#include<stdio.h>

int f(int,int);
int main()
{

printf("%d",f(20,1));
return 0;
}
int f(int n,int k)
{
if(n==0) return 0;
else if(n%2)return f(n/2,2*k)+k;
else return f(n/2,2*k)-k;
}
how this program is working and generating output as 9....?



#include<stdio.h> int f(int,int); int main() { printf("%d",f(20,1)); re..

Answer / kodam

n=20, k =1
if, else if false. so it will call
f(n/2,2*k)-k ==> f(10,2)-1
if, else if false
f(n/2,2*k)-k ==> f(5, 4)-2
if is false. else if is true
f(n/2,2*k)+k ==> f(2, 8)+4
if, else if false
f(n/2,2*k)-k ==> f(1, 16)-8
if is false. else if is true
f(n/2,2*k)+k ==> f(0, 32)+16
now n is zero.

output
------
-1-2+4-8+16 = 9

Is This Answer Correct ?    7 Yes 0 No

Post New Answer

More C Interview Questions

what is the output? #define fun(a,b,t) (g ##t=(a),(a)=(b),(b)=g##t) float gfloat; main() { float a=1.12,b=3.14; fun (a,b,float); printf("na=%4.2f,b=%4.2f",a,b); } A)Error in Defining Macro B)a=1.12,b=3.14 C)a=3.14,b=1.12 D)None of the Above

3 Answers   Accenture, Infosys, Wipro,


What are qualifiers in c?

0 Answers  


what are bitwise shift operators?

4 Answers  


What is the size of empty structure in c?

0 Answers  


while initialization of array why we use a[][2] why not a[2][]...?

0 Answers   Aptech,


Is the below things valid & where it will be stored in memory layout ? static const volatile int i; register struct { } ; static register;

2 Answers   Bosch,


void main() { int x=25,y=32; clrscr(); x=x++ + y++; y=++x + ++y; printf("%d%d",x,y); }

5 Answers  


what is differnence b/w macro & functions

1 Answers  


How do I convert a string to all upper or lower case?

0 Answers  


why little endian and big endian came?y they using differently? y they not used commonly ?wt is application of little and big ?

1 Answers  


What is the difference between the local variable and global variable in c?

0 Answers  


void main(int argc,char *argv[],char *env[]) { int i; for(i=1;i<argc;i++) printf("%s",env[i]); }

3 Answers  


Categories