C++ bootcamp · Lab 04

Even or odd

easyIf Else8 minLesson: If Else

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

QuestionHint and solution stay closed until you open them

Read one integer. Print even or odd (lowercase).

The modulo operator % gives the remainder — the classic even/odd test.

Input. One line: an integer (may be negative).

Output. The word even or odd.

Examples

Example 1
Input
4
Output
even
Example 2
Input
7
Output
odd
Hint
  1. n % 2 == 0 is true for even numbers.
  2. A ternary is compact: (n % 2 == 0 ? "even\n" : "odd\n").
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;
  std::cout << (n % 2 == 0 ? "even\n" : "odd\n");
  return 0;
}
main.cppC++17 · g++ · Ctrl + Enter runs
ResultIdle
Run to see output. Check grades the tests.