ProgrammingHardWipro · Cognizant pattern
What is printed?
What is printed?
int mystery(int a, int b) {
if (b == 0) return a;
return mystery(b, a % b);
}
int main(void) {
printf("%d", mystery(48, 18));
}- A. 48
- B. 12
- C. 6
- D. 18
Correct answer: C. 6
This is the Euclidean GCD algorithm: mystery(48,18) -> mystery(18, 48%18=12) -> mystery(12, 18%12=6) -> mystery(6, 12%6=0) -> returns 6. GCD(48,18) = 6.
More C Programming questions
- What is printed? (Assume int is 32 bits, two's complement.) c unsigned int a = 0xF0F0F0F0; unsigned int b = a >> 4; printf("%X", b);
- What is printed? (Assume int is 32 bits, two's complement, arithmetic right shift for signed negatives — the common but…
- What is printed? (Check if a number is a power of two.) c int isPowerOfTwo(int n) { return n > 0 && (n & (n - 1)) == 0; } int main(void) {…
- What is printed? c int swaptrick(int a, int b) { a = a ^ b; b = a ^ b; a = a ^ b; printf("%d %d ", a, b); return a + b; } int main(void) {…
- What is printed? c #define SETBIT(x, n) ((x) | (1 << (n))) #define CLEARBIT(x, n) ((x) & ~(1 << (n))) #define TOGGLEBIT(x, n) ((x) ^ (1 <<…
- What is printed? c #define SQUARE(x) x x int main(void) { int a = 5; printf("%d", SQUARE(a + 1)); }