#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 are the benefits of organizational structure?
Explain setjmp()?
Is return a keyword in c?
List at least 10 sorting methods indicating their average case complexity, worst case complexity and best case complexity.
What is the symbol indicated the c-preprocessor?
int i=10; printf("%d %d %d", i, i=20, i);
What does c mean before a date?
What is the difference between printf and scanf in c?
Can you think of a logic behind the game minesweeper.
What is the value of a[3] if integer a[] = {5,4,3,2,1}?
How can you determine the size of an allocated portion of memory?
Can you write the algorithm for Queue?
What is an expression?
How many bytes is a struct in c?
What are bitwise shift operators in c programming?