How to Find Odd and Even Number in Java

In java a number can be identified as odd or even by using modulo operator i.e. “%”. The modulo operator gives the remainder of a division operation, for instance the result of 5 % 3 would be 2. Also, any even number when divided by 2, leaves a remainder of zero. Thus, the algorithm would be if the remainder is 0, it is an even number else it is an odd number.

Below is the code snippet of this algorithm in java:

Odd_and_Even_Number_StudyorFun

public class OddEvenChecker
{

public static void main(String [] args)
{
if(args.length == 0)
{
System.out.println("Provide the number");
System.exit(0);
}
int n = Integer.parseInt(args[0]);
if(n % 2 == 0)
{
System.out.prinlnt("Number is even");
}
else
{
System.out.println("Number is odd");
}
}
}

Thus using the remainder or modulo operator, it can be checked if a number is odd or even.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s