C bootcamp · Lab 38

Keep the evens

easyArrays10 minLesson: C Arrays

Read the question, write C on the right, then Run or Check.

QuestionHint and solution stay closed until you open them

The first number is n, then n integers. Print only the even values, in the same order, space-separated.

Zero is even. If none are even, print a blank line.

Input. n, then n integers.

Output. The even values, space-separated.

Constraints

  • 1 ≤ n ≤ 100

Examples

Example 1
Input
6
1 2 3 4 5 6
Output
2 4 6
Example 2
Input
3
1 3 5
Output
Hint
  1. if (x % 2 == 0) printf("%d ", x);
Show correct code

Peek only after you have tried. You can still Check your own version.

#include <stdio.h>

int main(void) {
  int n;
  if (scanf("%d", &n) != 1) return 1;
  for (int i = 0; i < n; i++) {
    int x;
    scanf("%d", &x);
    if (x % 2 == 0) printf("%d ", x);
  }
  printf("\n");
  return 0;
}
main.cC17 · gcc · Ctrl + Enter runs
ResultIdle
Run to see output. Check grades the tests.