Odd or even in java

Even or Odd in java

Here in this post, we are going to write a program to print even and odd numbers in java. 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.

even odd number program in java : 

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.

 

nv-author-image

Anurag batra

A Tech Enthusiast who loves to work on his craft and make unique stuff From his Programming Skill at a same time Sharing that Knowledge with the outside world to help others.

Leave a Reply

avatar
  Subscribe  
Notify of