8371. Even or Odd

 

Given a positive integer n. Determine whether it is even or odd.

 

Input. One positive integer n (1 ≤ n ≤ 109).

 

Output. Print the string “EVEN” if the number n is even, and the string “ODD” if it is odd.

 

Sample input 1

Sample output 1

3

ODD

 

 

Sample input 2

Sample output 2

100

EVEN

 

 

SOLUTION

conditional statement

 

Algorithm analysis

Use a conditional statement to determine the parity of a number.

 

Algorithm implementation

Read the input value of n.

 

scanf("%d",&n);

 

Check whether the number n is even or odd. Based on the result, print the corresponding answer.

 

if (n % 2 == 0)

  printf("EVEN\n");

else

  printf("ODD\n");

 

Algorithm implementation – ternary operator

 

#include <stdio.h>

 

int n;

 

int main(void)

{

  scanf("%d",&n);

  (n % 2 == 0) ? puts("EVEN") : puts("ODD");

  return 0;

}

 

Algorithm implementation – switch

 

#include <stdio.h>

 

int n;

 

int main(void)

{

  scanf("%d", &n);

  switch (n % 2 == 0)

  {

    case 1:

      puts("EVEN");

      break;

    case 0:

      puts("ODD");

  }

  return 0;

}

 

Java implementation

 

import java.util.*;

 

public class Main

{

  public static void main(String[] args)

  {

    Scanner con = new Scanner(System.in);

    int n = con.nextInt();

    if (n % 2 == 0)

      System.out.println("EVEN");

    else

      System.out.println("ODD");         

    con.close();

  }

}

 

Java implementation – ternary operator

 

import java.util.*;

 

public class Main

{

  public static void main(String[] args)

  {

    Scanner con = new Scanner(System.in);

    int n = con.nextInt();

    System.out.println((n % 2 == 0) ? "EVEN" : "ODD");

    con.close();

  }

}

 

Java implementation – switch

 

import java.util.*;

 

public class Main

{

  public static void main(String[] args)

  {

    Scanner con = new Scanner(System.in);

    int n = con.nextInt();

    switch (n % 2)

    {

      case 0:

        System.out.println("EVEN");

        break;

      case 1:

        System.out.println("ODD");      

    }

    con.close();

  }

}

 

Python implementation

Read the input value of n.

 

n = int(input())

 

Check whether the number n is even or odd. Based on the result, print the corresponding answer.

 

if n % 2 == 0:

  print("EVEN")

else:

  print("ODD")