C++ bootcamp · Lab 25

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 line: an 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 <iostream>

int main() {
  int n;
  std::cin >> n;
  for (int i = 1; i <= n; i++) {
    if (i % 3 == 0) continue;
    std::cout << i << " ";
  }
  std::cout << "\n";
  return 0;
}
main.cppC++17 · g++ · Ctrl + Enter runs
ResultIdle
Run to see output. Check grades the tests.