Java program to check whether a given number is even or odd

Write a java program to check whether a number is even or odd

In this program, enter any integer number as an input. Now we check its remainder with modulo operator by two. If remainder is zero the given. If remainder is 1 then the given number is odd. Hence, we show output according to the remainder.

Here is the source code of the java program to check if a given integer is odd or even. The java program is successfully compiled and run on a windows system. The program output is also shown below.

  • Program:

import java.util.Scanner;

public class EvenOdd {

    public static void main(String[] args) {

        Scanner reader = new Scanner(System.in);

        System.out.print("Enter a number: ");
        int num = reader.nextInt();

        if(num % 2 == 0)
            System.out.println(num + " is even");
        else
            System.out.println(num + " is odd");
    }
}

  • Output:

Enter a number: 16
16 is even

--- Second Example ---

Enter a number: 27
27 is odd

  • Visit:

Java program to print an Integer Here
Java program to add two integers Here
Java program to find ASCII value of a character Here
Java program to compute quotient and remainder Here
Java program to swap two numbers Here

0 comments