#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 |
who is first prime minister in india??
What does. int *x[](); means ?
What is the need of structure in c?
what is associativity explain what is the precidence for * and & , * and ++ how the folloing declaration work 1) *&p; 2) *p++;
Which weighs more, a gram of feathers or a gram of gold?
write a c program for greatest of three numbers without using if statment
Write a C program to accept a matrix of any size. Find the frequency count of each element in the matrix and positions in which they appear in the matrix
a character or group of characters that defines a register,or a part of storage a) memory b) byte c) address d) linear list
What are the different pointer models in c?
Does * p ++ increment p or what it points to?
How to reverse a string using a recursive function, without swapping or using an extra memory?
31 Answers Cisco, Mind Tree, Motorola, Ophio, Sony, TCS, Wipro,
Write the syntax and purpose of a switch statement in C.