Programming Tutorials

Java program to check if user input is an even number

By: Paawan Chaudhary in Java Tutorials on 2012-09-19  

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);
		}
		
	}
}





Add Comment

* Required information
1000

Comments

No comments yet. Be the first!

Most Viewed Articles (in Java )

Latest Articles (in Java)