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:


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.

Please let us know for any queries or feedback in the comments section below.

  1. In open distributed systems the static assumptions of existing systems does not work and evolution is more difficult because it is not possible to control how or when the packages change and correct operation depends on a greater number of dependencies between packages. What is needed is an updated set of conventions and mechanisms that specify how the packages of a system should evolve so that the goal of an open reliable scalable distributed system can be achieved.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">