Find whether Even or odd number

Even number is an integer number which is "evenly divisible" by two. That means if the integer is divided by 2, it yield no remainder. For example, even numbers are, 2, 4, 6, 8, 10, etc.

Odd number is an integer number which is not a multiple of two. That means if it is divided by 2 the result is fraction. For example, 1, 3, 5, 7, 9, etc.

In the given program, we get a number from command line and calculate no%2. If it is zero then the number is even number otherwise that is odd number.


PROGRAM
class EvenOdd {

 public static void main(String[] args) {
  
  int num = Integer.parseInt(args[0]);
  
  if (num % 2 == 0) {
   System.out.println("Number is Even number.");
  }
  else {
   System.out.println("Number is Odd number.");
  }
 }
}
OUTPUT 1
C:\>javac EvenOdd.java
C:\>java EvenOdd 12
Number is Even number.
OUTPUT 2
C:\>javac EvenOdd.java
C:\>java EvenOdd 45
Number is Odd number.

Comments

Popular posts from this blog

Program to define a class 'employee' with data members as empid, name and salary. Accept data for 5 objects using Array of objects and print it.

Define a class Student with four data members such as name, roll no.,sub1, and sub2. Define appropriate methods to initialize and display the values of data members. Also calculate total marks and percentage scored by student.

Program to draw Smiley in Applet