Examples
- Hello World Program in Java (Print Hello World in Java)
- Java Program to Add Two Numbers (Java Sum / Addition)
- Find Greatest of Three Numbers in Java (Largest Number Program)
- Prime Number Program in Java (Code to Check Prime or Not)
- Java Program for Fibonacci Series (Using for, while, recursion, scanner)
- Factorial Program in Java (Find Factorial of a Number in Java)
- How to Find Sum of Digits of a Number in Java?
- How to Reverse a Number in Java? Program & Examples
- How to Swap Two Numbers in Java? Programs With/Without Third Variable
- Even Odd Program in Java (Program to Check Number is Even or Odd)
- Vowel and Consonant Program in Java
- Java Program for Quadratic Equation (Find Roots With 3 Ways)
- Find Frequency of Characters in a String in Java (4 Ways)
- Remove Space from String in Java (Remove Whitespace)
- String Null Check in Java (String is Empty or Null) - 5 Ways
- How to Print String in Java? 6 Methods
- How to Get ASCII Value of Char in Java? Find ASCII Value
Even Odd Program in Java (Program to Check Number is Even or Odd)
Let’s learn and practice the even or odd program in Java with multiple approaches!
Identifying even and odd numbers is a fundamental operation in programming, widely used in various applications, such as conditional statements, data filtering, and mathematical computations.
Understanding different techniques to efficiently check whether a number is even or odd in Java is essential for writing versatile and robust code.
Here, we have mentioned different approaches. Each method offers unique insights into handling number divisibility and bitwise operations, enabling you to choose the most suitable approach for different scenarios and optimize your code for better performance.
Java Concepts to Learn for This Program:
So, let’s get started and write the odd even prpgram in Java.
Check Number is Even or Odd in Java
Here's a Java program to check if a number is even or odd:
Code
import java.util.Scanner;
public class EvenOddChecker {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter an integer: ");
int number = scanner.nextInt();
if (isEven(number)) {
System.out.println(number + " is an even number.");
} else {
System.out.println(number + " is an odd number.");
}
}
private static boolean isEven(int num) {
return num % 2 == 0;
}
}
Output
Enter an integer: 93
93 is an odd number.
Explanation
In this program, we use the Scanner class to take an integer input from the user. The user is prompted to enter an integer, and then the program checks if the number is even or odd using the isEven method.
The isEven method takes an integer num as input and returns true if the number is even (i.e., divisible by 2 with a remainder of 0) and false otherwise.
Depending on the result of the isEven method, the program displays whether the given number is even or odd.
Even Odd Program in Java Using for loop
Below is a Java program to check if a given number is even or odd using a for loop:
Code
import java.util.Scanner;
public class EvenOddChecker {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter an integer: ");
int number = scanner.nextInt();
if (number < 0) {
System.out.println("Please enter a non-negative integer.");
} else {
boolean isEven = isEven(number);
if (isEven) {
System.out.println(number + " is an even number.");
} else {
System.out.println(number + " is an odd number.");
}
}
}
private static boolean isEven(int num) {
for (int i = 0; i <= num; i += 2) {
if (i == num) {
return true; // The number is even
}
}
return false; // The number is odd
}
}
Output
Enter an integer: 32
32 is an even number.
Explanation
In this program, we use a for loop to check if a given number is even or odd. The user is prompted to enter an integer, and the program checks if the number is non-negative. If the number is non-negative, the isEven method is called to determine if the number is even or odd.
The isEven method takes an integer num as input and iterates from 0 to num in steps of 2 (since even numbers are divisible by 2). If the loop variable i matches the input number num, it means the number is even, and the method returns true. Otherwise, it continues the loop and eventually returns false, indicating that the number is odd.
Even Number Program in Java
To check if a number is even or not in Java, you can use the modulo operator %. Here's a simple Java program to do that:
Code
import java.util.Scanner;
public class EvenOddChecker {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter an integer: ");
int number = scanner.nextInt();
if (isEven(number)) {
System.out.println(number + " is an even number.");
} else {
System.out.println(number + " is not an even number.");
}
}
private static boolean isEven(int num) {
return num % 2 == 0;
}
}
Output
Enter an integer: 66
66 is an even number.
Explanation
In this program, we use the Scanner class to take an integer input from the user. The user is prompted to enter an integer, and then the program checks if the number is even or not using the isEven method.
The isEven method takes an integer num as input and returns true if the number is even (i.e., divisible by 2 with a remainder of 0) and false otherwise.
Depending on the result of the isEven method, the program displays whether the given number is even or not.
Odd Number Program in Java
To check if a number is odd or not in Java, you can use the modulo operator %. Here's a simple Java program to do that:
Code
import java.util.Scanner;
public class OddEvenChecker {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter an integer: ");
int number = scanner.nextInt();
if (isOdd(number)) {
System.out.println(number + " is an odd number.");
} else {
System.out.println(number + " is not an odd number.");
}
}
private static boolean isOdd(int num) {
return num % 2 != 0;
}
}
Output
Enter an integer: 18
18 is not an odd number.
Explanation
In this program, we use the Scanner class to take an integer input from the user. The user is prompted to enter an integer, and then the program checks if the number is odd or not using the isOdd method.
The isOdd method takes an integer num as input and returns true if the number is odd (i.e., not divisible by 2 with a remainder of 0) and false otherwise.Depending on the result of the isOdd method, the program displays whether the given number is odd or not. If the number is not odd, it means it is either even or zero, as zero is neither odd nor even.