Even Odd in JAVA | Java Practice Question – 5

Write a Java Program that reads an integer and determines and prints whether it's odd or even.
https://youtu.be/_StfPO_Tfh4

Firstly, We have to import Java’s predefined Scanner Class from package java.util

import java.util.Scanner;

Now, Begins the declaration of class EvenOdd. and remember, the file name for this public class must be EvenOdd.java

public class EvenOdd

Note – Don’t forget an opening and closing braces.

Create the main method of java. that is available on every java program

public static void main(String[] args)

Note – Don’t forget an opening and closing braces.

Declaring and Creating a Scanner to Obtain User Input from the Keyboard.

Scanner input = new Scanner(System.in);

Declaring Variable to Store Integers

int inputNumber;

Prompting the User for input and Obtaining an int as input from the user

System.out.print("Enter Digit to check-");
inputNumber = input.nextInt();

It’s time to Apply the Even Odd Rule. i.e. Even is Divisible by 2 but, Odd is not.

if (inputNumber % 2 == 0){
  System.out.print("Even Number");
}
else
  System.out.print("odd Number");

Complete Source Code

import java.util.Scanner;
public class EvenOdd {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int inputNumber;
        System.out.print("Enter Digit to check -->");
        inputNumber = input.nextInt();

        if (inputNumber % 2 == 0){
            System.out.println("Even Number");
        }
        else{
            System.out.println("Odd Number");
        }
    }
}

Try more Java Practice Question

Leave a Reply

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