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....?



#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

Write a C program to perform some of the operation which can be performed using Single linked list

1 Answers   Qualcomm,


Is c compiled or interpreted?

0 Answers  


what is the difference between #include<stdio.h> and #include "stdio.h" ?

3 Answers  


do ne body have any idea about the salary for the we r going to have interview. yup .. u got it right ..i m talking abt NIC.

1 Answers  


void main() {int i=2; printf("%d%d%d",i,++i,i++); getch(); }

9 Answers  


Where static variables are stored in c?

0 Answers  


When do you not use the keyword 'return' when defining a function a) Always b) Never c) When the function returns void d) dfd

0 Answers  


What character terminates all strings composed of character arrays? 1) 0 2) . 3) END

3 Answers  


Explain #pragma in C.

1 Answers  


how to write optimum code to divide a 50 digit number with a 25 digit number??

0 Answers   MGM,


Diff: between this 2 classes in terms of memory class A { int i; char c; double d; }; class A { double d; int i; char c; }; How it is calculating?

1 Answers   HCL,


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

0 Answers  


Categories