ProgrammingHardProduct company OA · GATE CS
What is printed?
What is printed?
int m = 0;
long p(long b, int e) {
if (e == 0) return 1;
long h = p(b, e / 2);
m++;
if (e % 2) { m++; return h * h * b; }
return h * h;
}
int main(void) {
long r = p(3, 13);
printf("%ld %d", r, m);
}- A. 1594323 7
- B. 1594323 4
- C. 1594323 13
- D. 1594323 5
Correct answer: A. 1594323 7
e goes 13 → 6 → 3 → 1 → 0. m counts multiplications: two at each odd level (13, 3, 1) and one at the even level 6, so 2 + 1 + 2 + 2 = 7, far fewer than the 12 of the naive loop. 3^13 = 1594323. '4' counts calls, not multiplications.
More C Programming questions
- What is printed? c enum Flag { RED = 1, GREEN = RED << 1, BLUE = GREEN << 1, ALL = RED | GREEN | BLUE }; printf("%d %d", ALL, ALL & ~GREEN);
- What is printed? c void rev(char s, int i, int j) { if (i >= j) return; char t = s[i]; s[i] = s[j]; s[j] = t; rev(s, i + 2, j - 2); } int…
- Why are multi-statement macros usually written as #define M(x) do { ... } while (0)?
- What is printed? c #define INC(x) x++; x++ int a = 0, b = 0; if (a > 5) INC(b); printf("%d", b);
- What is printed? c int x = 7 / 2 2.0; double y = 7 / 2.0 2; printf("%d %.1f", x, y);
- What is printed? c void move(int pp) { (pp)++; pp += 10; } int main(void) { int a[] = {1, 2, 3}; int p = a; move(&p); move(&p); printf("%d…