ProgrammingHardWipro · Cognizant pattern
What is printed?
What is printed?
#include <stdio.h>
void f(int n) {
if (n == 0) return;
for (int i = 0; i < n; i++) printf("*");
f(n - 1);
printf("|");
}
int main(void) {
f(3);
}- A. ***|**|*|
- B. *|*|*|
- C. ******|||
- D. ***|||
Correct answer: C. ******|||
Descending, f(3) prints 3 stars, f(2) prints 2 stars, f(1) prints 1 star (6 stars total) before the base case f(0) returns with no output. Unwinding then prints '|' three times, once per returning call: 6 stars followed by 3 bars.
More C Programming questions
- What is printed? c #include <stdio.h> int cmp = 0; int search(int a, int n, int key) { if (n == 0) return -1; cmp++; if (a[n - 1] == key)…
- What is printed? c #include <stdio.h> void f(int n) { if (n <= 0) { printf("B "); return; } if (n % 2 == 0) printf("E%d ", n); else…
- What is printed? c #include <stdio.h> int digitSum(int n) { if (n == 0) return 0; printf("%d ", n % 10); return n % 10 + digitSum(n / 10)…
- What is printed? c #include <stdio.h> void count(int n) { int local = n; if (n == 0) { printf("local=%d ", local); return; } count(n - 1)…
- What is printed? c #include <stdio.h> int f(int n); int g(int n) { if (n <= 0) return n; return f(n - 2); } int f(int n) { if (n <= 0)…
- What is printed? c #include <stdio.h> int odds = 0; void f(int n) { if (n == 0) return; if (!(n % 2)) { f(n - 1); return; } odds++; f(n…