BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java Program to Check Whether a Number is Even or Odd

Last Updated: September 9, 2022 by Chaitanya Singh | Filed Under: Java Examples

In this article, we will write two java programs to check whether a number is even or odd. If a number is perfectly divisible by 2 (number % 2 ==0) then the number is called even number, else the number is called odd number.

Perfectly divisible by 2 means that when the number is divided by 2 the remainder is zero. To check for remainder, we use modulus operator in java.

Example 1: Program to check if the number entered by user is even or odd

In this program, we have an int variable num. We are using Scanner class to get the number entered by the user.

Once the entered number is stored in the variable num, we are using if..else statement to check if the number is perfectly divisible by 2 or not. Based on the outcome, it is executing if block (if condition true) or else block (if condition is false).

import java.util.Scanner;
public class JavaExample
{
  public static void main(String args[])
  {
    int num;
    System.out.print("Enter an Integer number: ");

    //The input provided by user is stored in num
    Scanner input = new Scanner(System.in);
    num = input.nextInt();

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

Output 1:
Java Program even odd output
Output 2:
Java Program even odd output2

Example 2: Program to check even odd number using ternary operator

A ternary operator is a one liner replacement of an if-else statement. In this program, we are using the same logic that we have seen above, however instead of using if..else, we are using a ternary operator here.

import java.util.Scanner;
public class JavaExample {

  public static void main(String[] args) {

    Scanner scan = new Scanner(System.in);

    System.out.print("Enter any number: ");
    int num = scan.nextInt();

    //checking if else using ternary operator
    //ternary operator syntax: condition ? expression1 : expression2
    // if condition is true, expression1 executes else expression2
    String evenOrOdd = (num % 2 == 0) ? "even number" : "odd number";

    System.out.println(num + " is an " + evenOrOdd);

  }
}

Output 1: When user enters 32 number.

Enter any number: 32
32 is an even number

Output 2: When user enters 13 number.

Enter any number: 13
13 is an odd number

Recommended Posts

  • Java program to find average of three numbers
  • Java program to calculate power of a number
  • Java program to print alternate prime numbers
  • Java program to find square root of a number without sqrt() method
❮ Java ProgrammingJava Programs ❯

Top Related Articles:

  1. Java Program to Calculate average using Array
  2. Sphenic Number in Java – Check and Print all numbers in a range
  3. Sunny Number Program in Java
  4. Java Program to Perform Arithmetic operation using Method Overloading
  5. java program to find factorial of a given number using recursion

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Comments

  1. kaushik says

    November 14, 2015 at 3:25 AM

    there are errors in the program its saying that FILE DOES NOT CONTAIN CLASS SCANNER :\

    Reply
    • Mohammad Yahiya Khan says

      August 1, 2021 at 8:46 PM

      Bro check your compiler or write import java.util.*; instead of Scanner then it will get compiled.

      Reply
  2. arul says

    December 14, 2015 at 7:11 AM

    if i run any programs like usergetinputs it shows the following error
    ” class java.util.Scanner not found in import.”
    please help me to correct this error thank you.

    Reply
    • Sultan says

      April 29, 2016 at 2:02 PM

      import package java.util.Scanner;
      Then it will not show any error..!

      Reply
    • Shubh Gupta says

      August 8, 2021 at 6:36 AM

      Pls type import java.util.*; on the top of the program above the public class

      Reply
    • Vraj says

      December 17, 2021 at 12:40 PM

      Use import java.util.*;

      This will 100% 😉 work

      Reply
  3. Rukhsaar says

    March 16, 2017 at 1:07 AM

    I tried this and there were no errors whatsoever .

    Reply

Leave a Reply Cancel reply

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

Java Examples

  • Check Odd-even
  • Linear Search
  • Binary Search
  • Floyd's Triangle
  • Reverse number
  • Random Number
  • first n prime numbers
  • Disp prime Numbers
  • Check Prime number
  • Palindrome String
  • Find factorial
  • Sum of elements of Array
  • Area of rectangle
  • Area of Square
  • Area of Triangle
  • Circle

Tutorials

  • Java Tutorial
  • OOPs Concepts
  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap