ProgrammingHardHCL · Tech Mahindra pattern

What is printed?

What is printed?
int f(int n) {
    if (n == 0) return 0;
    printf("%d ", n % 10);
    return f(n / 10);
}
int main(void) {
    f(4567);
}
  1. A. 4 5 6 7
  2. B. 7654
  3. C. 4567
  4. D. 7 6 5 4

Correct answer: D. 7 6 5 4

Each call prints the current last digit (n % 10) BEFORE recursing on n / 10 (the remaining digits). Starting from 4567: prints 7, recurses on 456; prints 6, recurses on 45; prints 5, recurses on 4; prints 4, recurses on 0 (base case, prints nothing). Output in call order: 7 6 5 4 — the digits come out reversed because recursion naturally processes them from least to most significant without any explicit reversal step.

More C Programming questions

Exams that ask this topic