• edited by
13,654 views
33 33 votes

What is the output printed by the following program?

#include <stdio.h>

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;
}

int main () {
    printf("%d", f(20, 1));
    return 0;
}
  1. $5$
  2. $8$
  3. $9$
  4. $20$

3 Answers

Best answer
46 46 votes

See the following calling sequence. Boxed values show the return values.

Hence, answer is option C.

• edited by
13 13 votes
The sequence has to be followed.

6.) f(20,1) = 9.

5.) f(10,2) - 1 = 9

4.) f(5,4) - 2 = 10

3.) f(2,8) + 4 = 12

2.) f(1,16) - 8 = 8

1.) f(0,32) + 16 = 16
Answer:
Position:
Show:

Related questions

70 70 votes
6 answers 6 answers
20.3k
20.3k views
Ishrat Jahan asked Nov 3, 2014
20,295 views
The following$ C$ function takes two ASCII strings and determines whether one is an anagram of the other. An anagram of a string s is a string obtained by permuting the l...
58 58 votes
8 8 answers
19.2k
19.2k views
Ishrat Jahan asked Nov 3, 2014
19,158 views
A user level process in Unix traps the signal sent on a Ctrl-C input, and has a signal handling routine that saves appropriate files before terminating the process. When ...
48 48 votes
8 answers 8 answers
13.3k
13.3k views
Ishrat Jahan asked Nov 3, 2014
13,263 views
A sink in a directed graph is a vertex i such that there is an edge from every vertex $j \neq i$ to $i$ and there is no edge from $i$ to any other vertex. A directed grap...
48 48 votes
5 answers 5 answers
16.4k
16.4k views
Ishrat Jahan asked Nov 3, 2014
16,367 views
A sink in a directed graph is a vertex i such that there is an edge from every vertex $j \neq i$ to $i$ and there is no edge from $i$ to any other vertex. A directed grap...