#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 / 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 |
What language is windows 1.0 written?
What is boolean in c?
What's wrong with the call "fopen ("c:\newdir\file.dat", "r")"?
1 What is a Data Structure?
triangle number finding program...
program to convert a integer to string in c language'
What is switch in c?
Write a program to exchange two variaables without temp
int main() { int x = (2,3,4); int y = 9,10,11; printf("%d %d",x,y); } what would be the output?
Explain argument and its types.
WHAT IS ABSTRACT DATA TYPE
How to declare pointer variables?