Author Topic: Java - Read Number and Check ODD or EVEN  (Read 811 times)

kathir

  • Administrator
  • Sr. Member
  • *****
  • Posts: 283
Java - Read Number and Check ODD or EVEN
« on: November 14, 2011, 03:27:54 pm »
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.
 

Code:
//Source Code Listing

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

 


Disclaimer: This web site is for educational and informational purposes only. Click here to read the Disclaimer.
Content copyright 2010-2014. Kathiresan. All rights reserved.
This page is using SMF version 2.0.1