Table of Contents
Program to check whether the given number is odd or even using operators in Java
In this tutorial, we will discuss the Program to check whether the number is odd or even using operators in Java
In this post, we are going to learn how to check whether the given number is odd or even using operator in Java language
Code to check whether the number is odd or even
Check whether the given number is odd or even using modular operators
Program 1
import java.util.Scanner; class CheckEvenOddModular{ public static void main (String args[]){ Scanner scan=new Scanner(System.in); System.out.print("Enter the number fo check odd or even: "); //ask input from the user int num=scan.nextInt(); //the input value stored in the variable num if(num%2==0)//using modular operator System.out.print(num+" is an even number"); else System.out.print(num+" is an odd number"); System.out.print("\n"); } }
When the above code is executed, it produces the following result
case 1
Enter the number to check odd or even: 5679 5679 is an odd number
case 2
Enter the number to check odd or even: 564 564 is an even number
Check whether the given number is odd or even using divisional operators
Program 2
import java.util.Scanner; class CheckEvenOddModular{ public static void main (String args[]){ Scanner scan=new Scanner(System.in); System.out.print("Enter the number fo check odd or even: "); //ask input from the user int num=scan.nextInt(); //the input value stored in the variable num if((num/2)*2==num)//using divisional operator System.out.print(num+" is a even number"); else System.out.print(num+" is a odd number"); System.out.print("\n"); } }
When the above code is executed, it produces the following resut
case 1
Enter the number to check odd or even: 7531 7531 is an odd number
case 2
Enter the number to check odd or even: 736 736 is an even number
Check whether the given number is odd or even using ternary operators
Program 3
import java.util.Scanner; class CheckEvenOddTernary{ public static void main (String args[]){ Scanner scan=new Scanner(System.in); System.out.print("Enter the number for check odd or even: "); //ask input from the user int num=scan.nextInt(); //the input value stored in the variable num String evenOdd=(num%2 ==0)? "even":"odd";//using ternary operator System.out.print(num+" is "+evenOdd); System.out.print("\n"); } }
When the above code is executed, it produces the following resut
Case 1
Enter the number for check odd or even:432 432 is even
Case 2
Enter the number for check odd or even:4321 4321 is odd
Check whether the given number is odd or even using bit wise operators
Program 4
import java.util.Scanner; class CheckOddEvenbitwise{ 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 & 1)==0){//using bit wise and operator 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 resut
case 1
Enter the integer number: 6756 6756 is a Even number
case 2
Enter the integer number: 1133 1133 is a Odd number
Check whether the given number is odd or even using shift operators
Program 5
import java.util.Scanner; class CheckOddEvenShift{ 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: "); //ask input from the user 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 resut
case 1
Enter the integer number: 100 100 is a Even number
case 2
Enter the integer number: 111 111 is a Odd number
Similar post
Java Program to check whether the number is odd or even using operators
C Program to check whether the number is odd or even using operators
C++ Program to check whether the number is odd or even using operators
Python Program to check whether the number is odd or even using operators
Suggested post
Data type and variable in Java language
variable types in Java language
Recursion in Java language