Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Beginners Check Even-Odd:-Example

 
 

Share on Google+Share on Google+

Check Even-Odd:-Example

Advertisement
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:-

import java.io.*;

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

Download Even-Odd Example

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 5, 2007

Related Tutorials

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

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:8
simon
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!!!
pradip
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"); } }
nessie
February 14, 2012
DATA STRUCTURE AND ALGORITHM

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

thanks for the codes ive learned much from it..... meralyn
jester
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
java

what is the purpose of try and catch command in this prgr thanx for example
Rob
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?
preetha
January 2, 2013
hi...........

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