Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training

Check Even-Odd:-Example

Now, you have to learn about the syntax of If-Else construct.


Check Even-Odd:-Example


Now, you have to learn about the syntax of If - Else construct. How the If - Else construct used to flow the program control as for as needed. If - Else construct has illustrated by a given example.

Given example reads a integer value and check weather number is Odd or Even. In this example you will learn how to determine the Even or Odd for the given number entered through the keyboard. If the given condition is true then print the message Given number is Even otherwise the control of the program will jumps in the else block and print the message Given number is Odd. In this example a special exception has been also used, that is NumberFormatException which holds the error during the checking the entered data format. Entered data must be a number not a character or string. If you enter anything except numeric value then normal flow of the program is sent to the catch block and print the specified message. Full running code is provided with the example.

Here is the code of the program:-


public class IfElse{
  public static void main(String[] args) throws IOException{
 int n;
 BufferedReader in = 
BufferedReader(new InputStreamReader(;
  n = Integer.parseInt(in.readLine());
 if (n % == 0)
  System.out.println("Given number is Even.");
  System.out.println("Given number is Odd.");
 catch(NumberFormatException e){
 " is not a numeric value.");

Download Even-Odd Example


Share on Google+Share on Google+

Check Even-Odd:-Example

Posted on: June 5, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Check Even-Odd:-Example   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
September 28, 2011
this is so nice

i like this website its really helping us as jonour programers who'll one day become senior progamers! i thank you!!!
December 23, 2011
Check Even-Odd numbers without modulo operator

class OddEven{ public static void main(String args[]) { int a,flag=0; a = Integer.parseInt(args[0].substring(args[0].length()-1,args[0].length())); for(int i=0;i<9; i=i+2) { if(a==i) { flag=1; } } if(flag==1) System.out.println("even"); else System.out.println("odd"); } }
February 14, 2012

what is the validation to this program.
July 1, 2012

thanks for the codes ive learned much from it..... meralyn
August 5, 2012
need help...

Specify number of input: 5 Enter 5 numbers 1 5 2 8 14 Numbers of odd numbers : 2 Numbers of even numbers : 3 can you make a program for this
ishwar chhetri
September 27, 2012

what is the purpose of try and catch command in this prgr thanx for example
October 13, 2012
Even / odd numbers - fastest algorithm

Most even/odd number checkers do it like this: if (number%2==0 then system.out.println('EVEN'); Although this is absolutely correct, we need to remember that computer can only add number, so the larger the given number is, the longer it is for the computer to come up with the answer assuming that %2 actually divides the number multiple times by 2). So, my question is how to make even/odd determination as fast as possible?
January 2, 2013

this soource code program was i think wrongly simplyfied............