#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

1. Write the function int countchtr(char string[ ], int ch); which returns the number of times the character ch appears in the string. Example, the call countchtr(“She lives in NEWYORK”, ‘e’) would return 3.

4 Answers  


pierrot's divisor program using c or c++ code

0 Answers  


What is the difference between array and structure in c?

0 Answers  


Explain what is the difference between far and near ?

0 Answers  


Write a program on swapping (100, 50)

0 Answers   BPL,






void main(int n) { if(n==0) return; main(--n); printf("%d ",n); getch(); } how it work and what will be its output...............it any one know ans plz reply

0 Answers  


What is union and structure in c?

0 Answers  


write a program to find the sum of the array elements in c language?

24 Answers   ICT, Infosys, Wipro,


how to make a scientific calculater ?

0 Answers  


What is #line used for?

0 Answers  


What is a ternary operator in c?

0 Answers  


What is the difference between void main and main in c?

0 Answers  


Categories