Table of Contents
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
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
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
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
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
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
Subtract two numbers using method overriding Program 1
PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…
Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…
Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…
Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…
How to write a program to convert Fahrenheit into Celsius In this article, we will…
This website uses cookies.