Back to challenges
Easy 0 solved 0% acceptance

Odd, Even, or Neither

Classify a number by whether it is an odd integer, an even integer, or neither.

What you'll learn

  • Check even and odd with % 2
  • Detect values that are not integers
  • Order conditions correctly

Problem

Whole numbers divisible by 2 are even; other whole numbers are odd. A number with a fractional part is neither. Zero is even, and negative integers follow the same rule.

Implement number_parity(number).

Input

number: The number to classify.

Output

Return odd, even, or neither as lowercase text.

Constraints

−1,000,000 ≤ number ≤ 1,000,000. The input is finite and has at most two decimal places.

Examples

Example 1
Input:
number = 14

Expected output:
expected = "even"
Input:
double number = 14;

Expected output:
std::string expected = "even";
Input:
double number = 14;

Expected output:
string expected = "even";

Explanation

14 = 2 × 7, so it is divisible by 2.

Example 2
Input:
number = 3.5

Expected output:
expected = "neither"
Input:
double number = 3.5;

Expected output:
std::string expected = "neither";
Input:
double number = 3.5;

Expected output:
string expected = "neither";

Explanation

3.5 has a fractional part, so it is neither an odd nor an even integer.

How to approach it

Try on your own first, then open one hint at a time.

Hint 1

First decide whether the input is a whole number, since fractional values are neither odd nor even.

Hint 2

For whole numbers, parity depends on whether division by 2 leaves a remainder.

Hint 3

Remember that zero is even, and negative integers follow the same rule as positive ones.

Expected complexity

O(1) time, O(1) extra space

References

This challenge is adapted for CodeUtility. Follow the statement and constraints above.

Learn from others

Community solutions

No published solutions yet. Solve this challenge to share the first one.