ProgrammingHardTCS NQT · Infosys pattern
What is printed?
What is printed?
int f(int n, int d) {
if (d > n) return 0;
if (n % d == 0) return d + f(n, d + 1);
return f(n, d + 1);
}
int main(void) { printf("%d", f(6, 1)); }- A. 6
- B. 9
- C. 3
- D. 12
Correct answer: D. 12
This sums all divisors of 6 from 1 upward: 1 (6%1==0) + 2 (6%2==0) + 3 (6%3==0) + 6 (6%6==0) = 12 (4,5 are skipped since they don't divide 6).
More C Programming questions
- What is printed? Assume no stack overflow for this depth. c int f(int n) { if (n == 0) return 0; if (n % 3 == 0) return 1 + f(n - 3)…
- What is printed? c int f(int n) { if (n == 0) return 0; int r = f(n - 1); printf("%d ", n); return r; } int main(void) { int x = f(4)…
- What is printed? c union U { char c; int i; double d; }; printf("%zu", sizeof(union U));
- Assume a 64-bit LP64 ABI. What is printed? c struct Inner { char c; int i; }; struct Outer { int x; struct Inner in; double d; }…
- On a typical x86-64 GCC/Clang target, what is printed? c struct F { unsigned a : 20; unsigned b : 20; }; printf("%zu", sizeof(struct F));
- Assume a 64-bit LP64 ABI. What is printed? c struct S { char c; int arr[2]; }; printf("%zu %zu", sizeof(struct S), offsetof(struct S, arr));