Browse  |   Placement Papers  |   Company  |   Code Snippets  |   Certifications  |   Visa Questions Post Question  |   Post Answer  |   My Panel  |   Search  |   Articles  |   Topics  |   ERRORS new

 Categories  >>  Software  >>  Programming Languages  >>  C

 C Interview Questions C++ Interview Questions VC++ Interview Questions Delphi Interview Questions Programming Languages AllOther Interview Questions
Question
```#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....?```
Question Submitted By :: Ganesh
I also faced this Question!!     Rank Answer Posted By

Re: #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....?
# 1
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 ? 2 Yes 0 No
Kodam

  Question Asked @ Answers Other C Interview Questions #define min((a),(b)) ((a)<(b))?(a):(b) main() { int i=0,a[20],*ptr; ptr=a; while(min(ptr++,&a[9])<&a[8]) i=i+1; printf("i=%d\n",i);} 3 main() { char *p1="Name"; char *p2; p2=(char *)malloc(20); while(*p2++=*p1++); printf("%s\n",p2); } what is the output? Ramco 6 what would be the output of the follwing struct st { char name[20]; int i; float f; }; main() { struct st emp = {"forum"}; printf("%d %f",emp.i,emp.f); } 4 what is the different between data structure and data type? Ignou 1 what are the advantage and disadvantage of recursion 4 what is the mean of c languages. Polaris 1 what is the difference between : func (int list[], ...) or func (int *list , ....) - what is the difference if list is an array and if also if list is a pointer 2 main() { intj; while9j<=10) { printf("\n%d",j); j=j+1; } } 4 program to get the remainder and quotant of given two numbers with out using % and / operators? IBM 9 Program to display given 3 integers in ascending order N-Tech 1 what is a stack 3 CAN WE DEFINE ANY FUNCTION WITHIN A FUNCTION. 15 For more C Interview Questions Click Here