#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....?
Answer Posted / 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 View All Answers
What is self-referential structure in c programming?
What is the meaning of typedef struct in c?
Why cant I open a file by its explicit path?
Write a program to generate a pulse width frequency of your choise,which can be variable by using the digital port of your processor
Can you subtract pointers from each other? Why would you?
Tell me is null always defined as 0(zero)?
FILE PROGRAMMING
What is the advantage of c?
Explain what will the preprocessor do for a program?
How do you sort filenames in a directory?
What are pointers in C? Give an example where to illustrate their significance.
#define PRINT(int) printf("int = %d ",int) main() {< BR> intx,y,z; x=03;y=02;z=01; PRINT(x^x); z<<=3;PRINT(x); y>>=3;PRINT(y); }
What do you understand by normalization of pointers?
Differentiate between a for loop and a while loop? What are it uses?
What is output redirection?