ProgrammingHardCompetitive coding (CP)
How many pairs (i, j) with i < j have an even sum? Compute with this program.
How many pairs (i, j) with i < j have an even sum? Compute with this program.
#include <stdio.h>
int main(void) {
int a[] = {1, 2, 3, 4, 5}, c = 0;
for (int i = 0; i < 5; i++)
for (int j = i + 1; j < 5; j++)
if ((a[i] + a[j]) % 2 == 0) c++;
printf("%d", c);
}- A. 6
- B. 3
- C. 4
- D. 5
Correct answer: C. 4
Two numbers sum to an even value when both are odd or both are even. The odd values 1, 3, 5 give 3 pairs and the even values 2, 4 give 1 pair, for 4 in total.
More C Programming questions
- What is printed? c #include <stdint.h> #include <stdio.h> int main(void) { uint16t s = 0; for (int i = 1; i <= 1000; i++) s += i…
- What is printed? Assume uint64t is 64 bits. c #include <stdint.h> #include <stdio.h> int main(void) { uint64t r = 1; for (uint64t i = 1; i…
- What is printed? c #include <stdio.h> int main(void) { unsigned n = 2; if (n - 3 < 5) puts("small"); else puts("big"); }
- What is printed? c #include <stdio.h> int main(void) { int idx = -1; int a[3] = {1, 2, 3}; unsigned u = (unsigned)idx; printf("%d", u < 3…
- What is printed? c #include <stdio.h> int f(void) { static int s = 5; return s++; } int main(void) { int a = f(), b = f(), c = f()…
- What is printed? Assume char is 1 byte, int is 4 bytes with 4-byte alignment. c #include <stdio.h> struct S { char a[5]; int b; }; int…