Odd Even

Program to check whether the number is odd or even using operators in Java

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

Operator in Java language

if statement in Java language

For loop in Java language

Method in Java language

Recursion in Java language

 

Karmehavannan

Recent Posts

Subtract two numbers using method overriding

Subtract two numbers using method overriding   Program 1

3 months ago

PHP Star triangle Pattern program

PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…

3 months ago

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

1 year ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

1 year ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

1 year ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

1 year ago

This website uses cookies.