ProgrammingHardAccenture · Cognizant
What is printed?
What is printed?
int c = 0;
for (int i = 1; i <= 4; i++)
for (int j = i; j <= 4; j += i) c++;
printf("%d", c);- A. 16
- B. 10
- C. 8
- D. 7
Correct answer: C. 8
For each i the inner loop visits the multiples of i up to 4: 4 for i = 1, 2 for i = 2 (2, 4), and 1 each for i = 3 and i = 4. Total 8. '10' counts j from i to 4 in steps of 1.
More C Programming questions
- Assume 16-bit unsigned short and 32-bit int. What is printed? c unsigned short v = 0x1234; unsigned short w = (unsigned short)(v << 8 | v…
- What is printed? c void conv(int n, int b) { if (n >= b) conv(n / b, b); putchar("0123456789ABCDEF"[n % b]); } int main(void) { conv(255…
- A single C source file contains these three lines at file scope. What happens? c int x; int x; int x = 5;
- Assume 4-byte int, 8-byte double and natural alignment. What is printed? c struct T { double d; int i; }; struct T arr[3]; printf("%td"…
- What is printed? c int len(const char s) { return s ? 1 + len(s + 1) : 0; } int cnt(const char s, char c) { return !s ? 0 : (s == c) +…
- What is printed? c int dr(int n) { return n < 10 ? n : dr(n / 10 + n % 10); } int main(void) { printf("%d %d", dr(9875), dr(99999)); }