#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

how to find out the inorder successor of a node in a tree??

2 Answers   TCS, Yahoo,


main() { clrscr(); } clrscr();

6 Answers   ME, Wipro,


What are 'near' and 'far' pointers?

0 Answers  


Who developed c language and when?

0 Answers  


In the below code, how do you modify the value 'a' and print in the function. You'll be allowed to add code only inside the called function. main() { int a=5; function(); // no parameters should be passed } function() { /* add code here to modify the value of and print here */ }

1 Answers  






In which header file is the null macro defined?

0 Answers  


any limit on the number of functions that might be present in a C program a) max 35 functions b) max 50 functions c) no limit d) none of the above

0 Answers  


What is main function in c?

0 Answers  


What are the different types of constants?

0 Answers  


What is #pragma directive?how it is used in the program? what is its advantages and disadvantages?

2 Answers  


If the static variable is declared as global, will it be same as extern?

1 Answers   Samsung,


What is self-referential structure in c programming?

0 Answers  


Categories