Even or Odd Number

beginner Number Problems

Problem

Read one integer and print Even if it is divisible by 2, otherwise print Odd. The number can be negative or zero.

Constraints

  • -10^9 <= n <= 10^9

Examples

Input

4

Output

Even

Input

7

Output

Odd

Solutions

Best approachTime O(1)Space O(1)

A number is even when it divides by 2 with no remainder. For negative numbers Java, JavaScript and TypeScript give a negative remainder (-3 % 2 = -1), so the safe test is remainder == 0.

Same code works in Java810172125Basic features only, so nothing changes between versions.
/*
 * Problem : Check if a number is even or odd.
 * Input   : One integer.
 * Output  : Even or Odd.
 * Approach: Check the remainder when dividing by 2.
 * Time    : O(1)
 * Space   : O(1)
 * Java    : 8 or newer.
 */
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        int number = new Scanner(System.in).nextInt();

        // Step 1: Find the remainder when the number is divided by 2.
        int remainder = number % 2;

        // Step 2: Even numbers have remainder 0. Odd numbers have remainder 1 or -1.
        // In Java, -3 % 2 is -1, so we compare with 0 and not with 1.
        if (remainder == 0) {
            System.out.println("Even");
        } else {
            System.out.println("Odd");
        }
    }
}
Hints (2)
  1. Use the % operator.
  2. Do not test remainder == 1: it fails for negative odd numbers in Java.