Use a conditional statement to determine the parity of a number.
Algorithm implementation
Read the input value of .
scanf("%d",&n);Check whether the number 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 = int(input())Check whether the number is even or odd. Based on the result, print the corresponding answer.
if n % 2 == 0:
print("EVEN")
else:
print("ODD")