Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

#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


Please Help Members By Posting Answers For Below Questions

What is scope rule in c?

1035


When should a type cast not be used?

1027


any C program contains only one function, it must be a) void () b) main () c) message () d) abc ()

1095


What is optimization in c?

964


Explain the properties of union.

1044


How can I write a function analogous to scanf?

1133


How will you print TATA alone from TATA POWER using string copy and concate commands in C?

1346


What is the use of bit field?

1148


Can a pointer be volatile in c?

962


What does it mean when a pointer is used in an if statement?

1077


Explain how can I manipulate strings of multibyte characters?

1176


Explain how can you tell whether a program was compiled using c versus c++?

1071


What is difference between class and structure?

1088


Give a one-line C expression to test whether a number is a power of 2. [No loops allowed - it's a simple test.]

1042


Tell me is null always defined as 0(zero)?

1056