Write a function that returns true or false.
What to build
A function is_even(n).
Requirements
- Write
bool is_even(int n). - For each of the numbers, print
evenorodd.
Input
The first line has q. The next line has q whole numbers.
Output
One word per number.
Constraints
q is from 1 to 100.
Example
Input:
3
1 2 3
Output:
odd
even
odd
Why it works
A bool return value lets the function be used directly as a condition.
Examples
Example 1 · Input
3 1 2 3
Expected output
odd even odd
Example 2 · Input
1 0
Expected output
even
Hints
Hint 1
Make the return type bool.
Hint 2
return n % 2 == 0; is all the body needs.
Solution
Show one correct solution (try it yourself first)
#include <iostream>
using namespace std;
bool is_even(int n) {
return n % 2 == 0;
}
int main() {
int q;
cin >> q;
for (int i = 0; i < q; i++) {
int n;
cin >> n;
cout << (is_even(n) ? "even" : "odd") << endl;
}
return 0;
}