Check value

5 ways to check whether the given integer is Even or Odd in Java

5 ways to check whether the given integer is Even or Odd in Java

In this tutorial, we will discuss the concept of  5 ways to check whether the given integer is Even or Odd in Java.

In this post, we are going to learn how to check a number is Even or Odd using various five ways in Java programming language

Check odd even using operator

5 ways to check whether the given integer is Even or Odd

Using modulo operator(%)

Program 1

 

import java.util.Scanner;
class CheckOddEven1{
public static void main(String args[]){
    Scanner scan=new Scanner(System.in); //create a scanner object for input
System.out.print("Enter the integer number: ");

int num=scan.nextInt();//get input from the user for num1
//using modular operator

int rem=num%2;
if(rem==0){
  System.out.println(num+" is a Even number");
}else{
 System.out.println(num+" is a Odd number");
}
}
}

When the above code is executed, it produces the following results

Case 1

Enter the integer number: 32
32 is a Even number

Case 2

Enter the integer number: 13
13 is a odd number

This program allows the user to enter a number and then, the program will check and display the odd or even numbers from the given number(entered by the user) using modular operator

 

Using division operator(/)

Program 2

import java.util.Scanner;
class CheckOddEven2{
public static void main(String args[]){
    Scanner scan=new Scanner(System.in); //create a scanner object for input
System.out.print("Enter the integer number: ");

int num=scan.nextInt();//get input from the user for num1
//using divisional operator
if((num/2) * 2==num){
  System.out.println(num+" is a Even number");
}else{
 System.out.println(num+" is a Odd number");
}
}
}

When the above code is executed, it produces the following results

Case 1

Enter the integer number: 99
99 is a odd number

Case 2

Enter the integer number: 50
50 is a even number

This program allows the user to enter a number and then, the program will check and display the odd or even numbers from the given number(entered by the user) using division operator

 

Using betwise AND operator(&)

Program 3

import java.util.Scanner;
class CheckOddEven3{
public static void main(String args[]){
    Scanner scan=new Scanner(System.in); //create a scanner object for input
System.out.print("Enter the integer number: ");

int num=scan.nextInt();//get input from the user for num1
//using Betwise AND operator

if((num&1)==0){
  System.out.println(num+" is a Even number");
}else{
 System.out.println(num+" is a Odd number");
}
}
}

When the above code is executed, it produces the following results

Case 1

Enter the integer number: 67
67 is a odd number

Case 2

Enter the integer number: 44
44 is a even number

This program allows the user to enter a number and then, the program will check and display the odd or even numbers from the given number(entered by the user) using bitwise AND operator

 

Using Left Shift and Right Shift operator(<<,>>)

Program 4

import java.util.Scanner;
class CheckOddEven4{
public static void main(String args[]){
    Scanner scan=new Scanner(System.in); //create a scanner object for input
System.out.print("Enter the integer number: ");

int num=scan.nextInt();//get input from the user for num1
//using shift operator

if(((num>>1)<<1)==num){
  System.out.println(num+" is a Even number");
}else{
 System.out.println(num+" is a Odd number");
}
}
}

 

When the above code is executed, it produces the following results

Case 1

Enter the integer number: 71
71 is a odd number

Case 2

Enter the integer number: 88
88 is a even number

This program allows the user to enter a number and then, the program will check and display the odd or even numbers from the given number(entered by the user) using shift operator

 

Using Ternary operator

Program 5

import java.util.Scanner;
class CheckOddEven5{
public static void main(String args[]){
    Scanner scan=new Scanner(System.in); //create a scanner object for input
System.out.print("Enter the integer number: ");

int num=scan.nextInt();//get input from the user for num1
//using ternary operator

String evenOdd=(num%2==0) ? "even":"odd";
System.out.println(num + " is "+evenOdd);
}
}

 

When the above code is executed, it produces the following results

Case 1

Enter the integer number: 143
143 is odd

Case 2

Enter the integer number: 266
266 is even

This program allows the user to enter a number and then, the program will check and display the odd or even numbers from the given number(entered by the user) using ternary operator

 

Suggested for you

The Operator in Java language

Data type and variable in Java language

Class and main method in Java language

 

Similar post

Program to find whether a Number is Prime or Not in C++

Program to find whether a Number is Prime or Not in C

Program to find whether a Number is Prime or Not in Java

Program to find whether a Number is Prime or Not in Python

 

C++ program to find first n prime numbers

Java program to find first n prime numbers

C program to find first n prime numbers

 

C code to 5 ways to check whether the given integer is Even or Odd

Java code to 5 ways to check whether the given integer is Even or Odd

C++ code to 5 ways to check whether the given integer is Even or Odd

Python code to 5 ways to check whether the given integer is Even or Odd

 

 

 

 

 

C++ program to find the odd or even number using switch statements
C code to 5 ways to check whether the given integer is Even or Odd
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

2 months ago