C Programming · Question 29 · Hard
What will be the output of the following C program?
#include <stdio.h>
void f(int n) {
if (n <= 0)
return;
f(n - 2);
printf("%d ", n);
f(n - 3);
}
int main(void) {
f(5);
return 0;
}
- 1 2 3 5
- 5 3 1 2
- 1 3 2 5
- 1 3 5 2
Answer and explanation
Answer: D. 1 3 5 2
Explanation: f(5) first calls f(3), which calls f(1); f(1) prints 1, so f(3) then prints 3 (its f(0) call prints nothing). Back in f(5), 5 is printed, and then f(2) prints 2. The output is 1 3 5 2.
Previous: What will be the output of the following C program? · Next: What will be the output of the following C program?
Practise c programming
Try a fresh set of c programming questions with an explanation after every answer, free and without signing up: C Programming practice. Or sit a full placement mock test.
More c programming questions
- What will be the output of the following C program?
- What will be the output of the following C program?
- What will be the output of the following C program?
- What will be the output of the following C program?
More technical & programming practice
Java · Python · Data Structures · Algorithms · Object-Oriented Programming · DBMS · SQL · Operating Systems
LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge