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);
}- A. 4 5 6 7
- B. 7654
- C. 4567
- 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
- What is printed? c void printPattern(int n) { if (n == 0) return; printPattern(n - 1); for (int i = 0; i < n; i++) printf(""); printf("…
- What is printed? c int f(int n) { if (n == 0) return 1; int a = f(n - 1); return a n; } int main(void) { long long result = 1; for (int i…
- What happens? c struct Node { int data; struct Node next; }; struct Node createNode(int val) { struct Node n; n.data = val; n.next = NULL…
- What is printed? c void outerFunc(void) { int x = 10; { int y = 20; printf("%d %d ", x, y); } // printf("%d", y); // line X } int…
- What is printed? c int idgenerator(void) { static int nextid = 100; return nextid++; } int main(void) { int a = idgenerator(); int b =…
- What is printed? c int applyTwice(int (f)(int), int x) { return f(f(x)); } int square(int x) { return x x; } int main(void) { printf("%d"…