Java Program to check Even or Odd number

Java Program to check Even or Odd number

Java program to check if a number is odd or even: Basic logic goes like if a number is divisible by two then it will be even, else, it Should odd. We use the modulus operator to find the remainder of a number in our program. For an even number, the remainder will be zero when it is divided by two and will be one for an odd number.

Java programming code : 

import java.util.Scanner;

class CheckEvenOdd
{
  public static void main(String args[])
  {
    int num;
    System.out.println("Enter an Integer number:");

    //Scanner is Used to take input from the User
    Scanner input = new Scanner(System.in);
    num = input.nextInt();

    /* If number is divisible by 2 then it is an even number
     * else odd number*/
    if ( num % 2 == 0 )
        System.out.println("Entered number is even");
     else
        System.out.println("Entered number is odd");
  }
}

output Of Java Program

odd even java

As you can See in the Above Output We entered 5 and it classified it as Odd Number because when 5 is Divided by 2 it leaves 1 as a reminder also 5 is an Odd number

 

The process of Running a Java program is stated below:

  • Create the program by typing it into a text editor and saving it to a file – CheckEvenOdd.java.
  • Compile it by typing “javac CheckEvenOdd.java” in the terminal window.
  • Execute (or run) it by typing “java CheckEvenOdd” in the terminal window.

 

Leave a Reply

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