Free CoreJava Programs Source codes | Examples | Video Tutorials Download

Tuesday, January 20, 2009

Odd Even Find Program

/**
* Write a program to accept a number from the command line and determine whether
* it is an odd or an even number
*/
class OddEven
{
/**
* excution begin from here
* @param args : command line argument
*/
public static void main(String[] args)
{
if(args.length!=1)
{
System.out.println("Invalid Argument!!!");
return;
}

byte res=isNoOdd(args[0]);
if(res==0)
System.out.println(args[0] + " is Invalid Input!!");
else if(res==1)
System.out.println(args[0] + " is odd");
else if(res==2)
System.out.println(args[0] + " is even");
}

/**
* check if given no is odd or not
* @param no : input number
* @return : 0 for Error or Zero
* 1 for odd
* 2 for even
*/
public static byte isNoOdd(String no)
{
try
{
int number=Integer.parseInt(no);
if(number==0)
return 0;
if(number%2==0)
return 2;
else
return 1;
}
catch(Exception e)
{
//System.out.println("Error Occured!!");
//e.printStackTrace();
return 0;
}
}
}

No comments:

Post a Comment

Followers