Algorithm Analysis
We use a conditional statement to determine the parity of a number.
Algorithm Implementation
Read the input number .
scanf("%d", &n);
Depending on the parity of , 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(); } }