ProgrammingModerateHCL · Tech Mahindra pattern
What does f(4096) return?
What does f(4096) return?
int f(int n) {
if (n == 0) return 0;
return n % 10 + f(n / 10);
}- A. 10
- B. 4096
- C. 19
- D. 15
Correct answer: C. 19
The function adds the last digit and recurses on the rest: 6 + 9 + 0 + 4 = 19, ending when n becomes 0. It is the digit-sum routine.
More C Programming questions
- What is printed? c printf("%d %d %d", 12 & 10, 12 | 10, 12 ^ 10);
- What is printed? c #define SQUARE(x) x x printf("%d", SQUARE(2 + 3));
- What is printed when count() is called three times in a row? c void count(void) { static int c = 0; c++; printf("%d ", c); }
- What is printed? c int a[5] = {1, 2}; printf("%d", a[3]);
- What is printed? c int x = 5; printf("%d", x > 3 ? x < 10 ? 1 : 2 : 3);
- What is printed? c int i; for (i = 0; i < 10; i += 3) printf("%d ", i);