Odd Even Number
Java Program To Check Whether a Number is Odd or Even
To check if a number is even or odd,the number is divided by 2 and is checked if it’s divisible or not. If number is divisible by 2 it’s even else it’s odd.
Three are three ways to check whether a number is even or odd
Method 1 : Using Brute Force
Method 2 : Using Ternary Operator
Method 3 : Using Bitwise Operators
Program 1 : Java Program To Check Whether a Number is Even or Odd Using Brute Force
import java.util.Scanner;
public class UsingBruteForce {
public static void main(String args[])
{
int num_to_check;
Scanner sc= new Scanner(System.in);
System.out.print("Enter the number: ");
num_to_check=sc.nextInt();
if (num_to_check % 2 == 0)
System.out.println(num_to_check + " is Even");
else
System.out.println(num_to_check + " is odd");
}
}
Output:
Enter the number: 19
19 is odd
Enter the number: 30
30 is Even
Program 2 : Java Program To Check Whether a Number is Even or Odd Using Ternary Operator
Ternary Operator Syntax :
( Condition ) ? ( if True : Action ) : ( if False : Action ) ;
import java.util.Scanner;
public class ternary_operator {
public static void main(String args[])
{
int num_to_check;
Scanner sc= new Scanner(System.in);
System.out.print("Enter the number: ");
num_to_check=sc.nextInt();
String status = num_to_check % 2 == 0 ? " is Even" : " is Odd";
System.out.println (num_to_check + status);
}
}
Output:
Enter the number: 37
37 is Odd
Enter the number: 56
56 is Even
Program 3 : Java Program To Check Whether a Number is Even or Odd Using Bitwise Operator
Program a : Java Program To Check Whether a Number is Even or Odd Using Bitwise AND operator
import java.util.Scanner;
public class bitwise_and_operator {
// Returns true if n is even, else odd
static boolean isEven(int n)
{
// n&1 is 1, then odd, else even
return ((n & 1) != 1);
}
public static void main(String[] args)
{
int num_to_check;
Scanner sc= new Scanner(System.in);
System.out.print("Enter the number: ");
num_to_check=sc.nextInt();
System.out.print(isEven(num_to_check) == true ? num_to_check + " is Even" : num_to_check + " is Odd");
}
}
Output:
Enter the number: 29
29 is Odd
Enter the number: 46
46 is Even
Program b : Java Program To Check Whether a Number is Even or Odd Using Bitwise XOR operator
public class bitwise_xor_operator {
// Returns true if n is even, else odd
static boolean isEven(int n)
{
// n^1 is n+1, then even, else odd
if ((n ^ 1) == (n + 1))
return true;
else
return false;
}
public static void main(String[] args)
{
int num_to_check;
Scanner sc= new Scanner(System.in);
System.out.print("Enter the number: ");
num_to_check=sc.nextInt();
System.out.print(isEven(num_to_check) == true ? num_to_check + " is Even" : num_to_check + " is Odd");
}
}
Output :
Enter the number: 56
56 is Even
Enter the number: 65
65 is Odd
Program c : Java Program To Check Whether a Number is Even or Odd Using Bitwise OR operator
public class bitiwise_or_operator {
// Returns true if n is even, else odd
static boolean isEven(int n)
{
// n|1 is greater than n, then even, else odd
if ((n | 1) > n)
return true;
else
return false;
}
public static void main(String[] args)
{
int num_to_check;
Scanner sc= new Scanner(System.in);
System.out.print("Enter the number: ");
num_to_check=sc.nextInt();
System.out.print(isEven(num_to_check) == true ? num_to_check + " is Even" : num_to_check + " is Odd");
}
}
Output :
Enter the number: 84
84 is Even
Enter the number: 95
95 is Odd