Java program to check if user input is an even number
By: Paawan Chaudhary
This is a java program to accept a number from the user and throw an exception if the number is not an even number.
import java.lang.*;
import java.io.*;
class myException extends Exception
{
myException(String msg)
{
super(msg);
}
}
class q12Exception
{
public static void main(String args[])
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n;
try
{
System.out.println ("Enter a no.");
n=Integer.parseInt(br.readLine());
if(n%2==0)
{
System.out.println ("No. is even");
}
else
{
throw new myException("No. is not even");
}
}
catch(myException me)
{
System.out.println (me);
}
catch(IOException ie)
{
System.out.println (ie);
}
}
}
Archived Comments
1. check this link http://www.msccomputerscience.com/2013/01/write-program-to-find-odd-or-even.html
View Tutorial By: arun at 2014-11-05 05:24:05
Comment on this tutorial
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Java program to get location meta data from an image
Program using concept of byte long short and int in java
Update contents of a file within a jar file
Tomcat and httpd configured in port 8080 and 80
Count number of vowels, consonants and digits in a String in Java
Student marks calculation program in Java
Calculate gross salary in Java
Calculate average sale of the week in Java
Vector in Java - Sample Program