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

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

1 month ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

2 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago