Write a program to check whether the given number is even or odd in Java
In this post, you will learn how to check whether a given number is even or odd in Java programming language.
To check whether a number is odd or even, you can either ask a user to enter a number, or you can provide a list from which the program can check whether the number is odd or even. Such a type of program is generally asked in programming interviews or in examinations.
A number is even if it is perfectly divisible by 2, and if the remainder is not zero, the number is odd.
Java program to check even or odd number using if statement
Here, we check whether num is even or odd by using the if... if...else statement in Java. We calculate its remainder using the % operator and check if it is divisible by 2 or not. If the num is divisible by 2, we print that the num is even. Otherwise, we print the num is odd.
public class OddEven_Program
{
public static void main(String[] args)
{
int num = 23;
if(num % 2 == 0)
{
System.out.println("The given number "+num+" is Even.");
}
else
{
System.out.println("The given number "+num+" is Odd.");
}
}
}
Output of the above code:
The given number 23 is Odd.
Java program to check even or odd number using ternary operator
The ternary operator is an operator that exists in some programming languages and takes three operands rather than the typical one or two that most operators use. Here, we have used the ternary operator to check the odd even number using the Java programming language.
public class OddEven_Program
{
public static void main(String[] args) {
int num = 103;
String evenOdd = (num % 2 == 0) ? "even" : "odd";
System.out.println(num + " is " + evenOdd);
}
}
Output of the above code:
The given number 23 is Odd.
Java program to check even or odd number using bitwise XOR
In the given Java program, we have used the bitwise XOR operator to check whether the number is even or odd. First, we have declared a variable to store the number. If the number after bitwise XOR with 1 is equal to the original number + 1, then it is an even number. If not equal, then it is an odd number.
public class OddEven_Program
{
public static void main(String[] args) {
int num = 107;
// Check using bitwise XOR
if ((num ^ 1) == num + 1)
{
System.out.println("The number "+ num +" is Even.");
}
else
{
System.out.println("The number "+ num +" is Odd.");
}
}
}
Output of the above code:
The number 107 is Odd.
Java program to check even or odd number using function
In the given Java program, we have checked whether the given number is odd or even using a function.
public class OddEven_Program
{
static void CheckOddEven(int num) {
if (num % 2 == 0){
System.out.println(num + " is an even number.");
} else if (num % 2 == 1) {
System.out.println(num + " is an odd number.");
}
}
public static void main(String[] args) {
CheckOddEven(44);
CheckOddEven(13);
CheckOddEven(5);
CheckOddEven(48);
CheckOddEven(7);
CheckOddEven(39);
}
}
Output of the above code:
44 is an even number.
13 is an odd number.
5 is an odd number.
48 is an even number.
7 is an odd number.
39 is an odd number.
Related Articles
Capitalize first letter of each word JavaConvert binary to decimal in Java
Electricity bill program in Java
Java program to find factorial of a number
Area of circle program in Java
Count vowels in a string Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java
Print prime numbers from 1 to 100 in Java
Java prime number program
Java program to convert celsius to fahrenheit
Fibonacci series program in Java
Java program to check leap year
Java program to find factorial of a number