Retour aux défis
Facile 0 résolus 0 % de réussite

Odd, Even, or Neither

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

Ce que vous allez apprendre

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

Problème

Afficher la traduction

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.

Contraintes

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

Exemples

Exemple 1
Entrée :
number = 14

Sortie attendue :
expected = "even"
Entrée :
double number = 14;

Sortie attendue :
std::string expected = "even";
Entrée :
double number = 14;

Sortie attendue :
string expected = "even";

Explication

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

Exemple 2
Entrée :
number = 3.5

Sortie attendue :
expected = "neither"
Entrée :
double number = 3.5;

Sortie attendue :
std::string expected = "neither";
Entrée :
double number = 3.5;

Sortie attendue :
string expected = "neither";

Explication

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

Comment l'aborder

Essayez d'abord seul, puis ouvrez les indices un par un.

Indice 1

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

Indice 2

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

Indice 3

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

Complexité attendue

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

Références

Ce défi a été adapté pour CodeUtility. Suivez l'énoncé et les contraintes ci-dessus.

Apprenez des autres

Solutions de la communauté

Aucune solution publiée pour l'instant. Résolvez ce défi pour partager la première.