C bootcamp · Lab 26

Skip multiples of 3

easyBreak Continue10 minLesson: Break Continue

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

QuestionHint and solution stay closed until you open them

Read n and print the integers from 1 to n that are not multiples of 3, space-separated.

Use continue to skip a loop body when i is divisible by 3.

Input. One integer n ≥ 1.

Output. Space-separated integers that are not multiples of 3.

Constraints

  • 1 ≤ n ≤ 50

Examples

Example 1
Input
10
Output
1 2 4 5 7 8 10
Hint
  1. if (i % 3 == 0) continue;
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 = 1; i <= n; i++) {
    if (i % 3 == 0) continue;
    printf("%d ", i);
  }
  printf("\n");
  return 0;
}
main.cC17 · gcc · Ctrl + Enter runs
ResultIdle
Run to see output. Check grades the tests.