- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to Check Whether a Number is Even or Odd
In this article, we will understand how to check whether a number is even or odd. This is accomplished by checking if the given number is divisible by 2.
Below is a demonstration of the same −
Input
Suppose our input is −
Enter the number : 45
Output
The desired output would be −
The number 45 is an odd number
Algorithm
Step 1 - START Step 2 - Declare an integer values namely my_input. Step 3 - Read the required values from the user/ define the values Step 4 - Use the modulus operator and compute my_input % 2 value. Step 5 - If the result is 0, then its an even number, else it’s an odd number. Step 5 - Display the result Step 6 - Stop
Example 1
Here, the input is being entered by the user based on a prompt. You can try this example live in our coding ground tool .
import java.util.Scanner; public class EvenOdd { public static void main(String[] args) { int my_input; System.out.println("Required packages have been imported"); Scanner my_scanner = new Scanner(System.in); System.out.println("A reader object has been defined "); System.out.print("Enter the number : "); my_input = my_scanner.nextInt(); if(my_input % 2 == 0) System.out.println("The number " +my_input + " is an even number"); else System.out.println("The number " +my_input + " is an odd number"); } }
Output
Required packages have been imported A reader object has been defined Enter the number : 45 The number 45 is an odd number
Example 2
Here, the integer has been previously defined, and its value is accessed and displayed on the console.
public class EvenOdd { public static void main(String[] args) { int my_input; my_input = 45; System.out.println("The number is defined as " +my_input); if(my_input % 2 == 0) System.out.println("The number " +my_input + " is an even number"); else System.out.println("The number " +my_input + " is an odd number"); } }
Output
The number is defined as 45 The number 45 is an odd number
- Related Articles
- C++ Program to Check Whether Number is Even or Odd
- 8085 program to check whether the given number is even or odd
- Java program to find whether given number is even or odd
- Python Program to Determine Whether a Given Number is Even or Odd Recursively
- Golang Program to Determine Recursively Whether a Given Number is Even or Odd
- Java Menu Driven Program to Check Positive Negative or Odd Even Number
- Check whether given floating point number is even or odd in Python
- Java Program to check if count of divisors is even or odd
- How to Check if a Number is Odd or Even using Python?
- PHP program to check if the total number of divisors of a number is even or odd
- Java Program to Check Whether a Number is Positive or Negative
- Java Program to Check Whether a Number is Prime or Not
- Program to check if a number is Positive, Negative, Odd, Even, Zero?
- C# Program to check if a number is Positive, Negative, Odd, Even, Zero
- C Program to Check if count of divisors is even or odd?

Advertisements