ProgrammingHardProduct company OA
What is printed?
What is printed?
int dr(int n) { return n < 10 ? n : dr(n / 10 + n % 10); }
int main(void) { printf("%d %d", dr(9875), dr(99999)); }- A. 11 18
- B. 2 9
- C. 29 45
- D. 2 0
Correct answer: B. 2 9
Replacing n by n / 10 + n % 10 never changes n modulo 9, so the recursion ends at the digital root. 9875: 992 → 101 → 11 → 2. 99999: 10008 → 1008 → 108 → 18 → 9 (a multiple of 9 ends at 9, not 0). '29 45' are the plain digit sums.
More C Programming questions
- What is printed? c unsigned x = 0xF0F0; x = x - ((x >> 1) & 0x5555); x = (x & 0x3333) + ((x >> 2) & 0x3333); x = (x + (x >> 4)) & 0x0F0F…
- What is printed? c struct N { int v, next; } t[] = {{10, 2}, {20, -1}, {30, 4}, {40, 1}, {50, 1}}; int s = 0; for (int i = 0; i != -1; i =…
- Given int x; const int pc; int p; int const cp = &x;, which assignment is a constraint violation that the compiler must diagnose?
- What is printed? c #define S(x) #x printf("%zu %zu", strlen(S("hi")), sizeof(S(ab)));
- What is printed? c int paths(int r, int c) { return (r == 0 || c == 0) ? 1 : paths(r - 1, c) + paths(r, c - 1); } int main(void) {…
- Assume 8-bit unsigned char. What is printed? c int n = 0; for (unsigned char c = 0; c < 300; c++) { n++; if (n > 1000) break; }…