ProgrammingModerateTCS NQT · Wipro
What is printed?
What is printed?
#include <stdio.h>
unsigned sumd(unsigned n) {
return n == 0 ? 0 : n % 10 + sumd(n / 10);
}
int main(void) {
printf("%u", sumd(98765));
}- A. 29
- B. 40
- C. 36
- D. 35
Correct answer: D. 35
The recursion adds the last digit and recurses on the remaining digits, giving 9 + 8 + 7 + 6 + 5 = 35. Stopping one digit early gives 29.
More C Programming questions
- How many subsets of {1, 2, 3, 4, 5, 6} (the empty set included) have a sum divisible by 3? Compute with this program. c #include <stdio.h>…
- What is printed by the first two calls? c #include <stdio.h> unsigned next(void) { static unsigned s = 1; s = s 1103515245u + 12345u…
- What is printed? c #include <stdio.h> int main(void) { int total = 0; for (unsigned x = 1; x <= 100; x++) { unsigned y = x; while (y) {…
- What is printed? c #include <stdio.h> #include <string.h> int main(void) { const char names[] = {"ann", "bob", "cat", "dan", "eve"}; const…
- What is printed? Assume long long is 64 bits and that the product fits in a long long. c #include <stdio.h> int main(void) { long long p =…
- What is printed? c #include <stdio.h> int clz16(unsigned short x) { int n = 0; while (!(x & 0x8000u)) { x <<= 1; n++; } return n; } int…