Java program to check if user input is an even number

By: Paawan Chaudhary Viewed: 154232 times  Printer Friendly Format    


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


Most Viewed Articles (in Java )

Latest Articles (in Java)

Comment on this tutorial