Java program to check whether a number is even or odd; if it's divisible by two, then it's even, otherwise, odd. We use the modulus operator to find the remainder. For an even number, it's zero when it's divided by two (it's one for an odd number).
Odd even program in Java
class OddOrEven
{
public static void main(String args[])
{
int x;
System.out.println("Enter an integer to check if it's odd or even");
Scanner in = new Scanner(System.in);
x = in.nextInt();
if (x % 2 == 0)
System.out.println("The number is even.");
else
System.out.println("The number is odd.");
}
}
Output of program:
Download Odd or even program class file.
Another method to check odd or even, for explanation see: C program to check odd or even. Code:
class EvenOdd
{
public static void main(String args[])
{
int c;
System.out.println("Input an integer");
Scanner in = new Scanner(System.in);
c = in.nextInt();
if ((c/2)*2 == c)
System.out.println("Even");
else
System.out.println("Odd");
}
}
There are other methods for checking odd/even one such way is using bit-wise operators.