In this tutorial you will learn about the Java Program to Check Whether a Number is Even or Odd and its application with practical example.
Java Program to Check Whether a Number is Even or Odd
In this tutorial, we will learn to create a Java program that will Check Whether a Number is Even or Odd using Java programming.
Prerequisites
Before starting with this tutorial we assume that you are best aware of the following Java programming topics:
- Operators in Java Programming.
- Basic Input and Output function in Java Programming.
- Basic Java programming.
- Conditional Statements in Java programming.
- Arithmetic operations in Java Programming.
What is an even and odd number?
Even numbers are those numbers that are completely divisible by 2. which means the reminder should be zero. And odd numbers are those numbers that cannot be completely divisible by 2.
Algorithm:-
1 2 3 4 5 6 7 |
1. Declaring the variables for the program. 2. Taking the input integer number from the user. 3. Checking the Even & Odd numbers from the input. 4. Printing the resulting number. 5. End Program. |
Program Description:-
In this program, First, we will take an input number from the user. And then we will use it in the program with conditional statements to find whether the number is even or odd. At last, we will print the output number as even or odd.
With the help of this program, we can Check Even and Odd numbers.
Program:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import java.util.Scanner; public class EvenOdd { public static void main(String[] args) { //Declaring the variable for the program to check even and odd number int no; //Taking the input number from the user Scanner reader = new Scanner(System.in); System.out.print("Enter a number: "); no = reader.nextInt(); //Checking the number even or odd if(no % 2 == 0) //Printing the even number in the output System.out.println(no + " is even"); else //Printing the odd number in the output System.out.println(no + " is odd"); } } |
Output:-
In the above program, we have first initialized the required variable.
- no = it will hold the integer value.
Input number from the user.
Checking if even number.
Printing the even and odd number.