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
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
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
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
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
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
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
Python code to Calculate sum of odd and even in a list In this tutorial,…
How to find reverse number using method In this article, we will discuss the concept…
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…