Java - Read Number and Check ODD or EVEN
I have given here a java program to read number and check ODD or EVEN. It uses mod (%) operator with 2. If the remainder is 1, then it would be an ODD number otherwise EVEN Number.
Source Code
import java.lang.*;
import java.util.*;
import java.io.*;
class Check_ODD_EVEN
{
public static int ReadInteger()
{
try
{
InputStreamReader input = new InputStreamReader(System.in);
BufferedReader reader = new BufferedReader(input);
return Integer.parseInt(reader.readLine());
}
catch (Exception e)
{
e.printStackTrace();
return 0;
}
}
public static void main(String[] args)
{
System.out.print("Enter a Number to Check ODD or EVEN: ");
int number = ReadInteger();
if((number % 2) == 0)
System.out.format("%d is an EVEN number\n", number);
else
System.out.format("%d is an ODD number\n", number);
}
}
Output
Enter a Number to Check ODD or EVEN: 15
15 is an ODD number
Press any key to continue . . .
Enter a Number to Check ODD or EVEN: 22
22 is an EVEN number
Press any key to continue . . .
|