Check value

Java code to check whether a number is even or odd

Java code to check whether a number is even or odd

In this tutorial, we will discuss a simple concept of Java code to check whether a number is even or odd

In this post, we are going to learn about how to find  odd or even number from given number using if else statements in Java programming language

 

What is even or odd

When the number is exactly divisible by 2, it is called as the even number

if the number is not exactly divisible by 2, it is called as the odd number

 

Program 1

Once This program received the number it will check the given number either odd or even.

After the program received input from the user, it is stored in the variable of num. then program divides the value of num into 2 and displays the output

Using modular operator

import java.util.Scanner;
class EvenOdd1{
public static void main (String args[]){
Scanner scan=new Scanner(System.in);//Scanner class
System.out.print("Enter a number: ");
int num=scan.nextInt();//get input from user
if(num%2==0)
    System.out.print(num+" is a even number");
else
   System.out.print(num+" is a odd number");
}
}

 

When the above code is executed, it produces the following results

case 1

Enter an integer value: 44
44 is a even number

case 2

Enter an integer value: 65
65 is a odd number

when we use the modular operator, if it is the remainder appears as 0, it is called even number and if the remainder displays the balance as one, it is called as odd or uneven number

Program 2

Using ternary operator

import java.util.Scanner;
class EvenOdd2{
public static void main (String args[]){
Scanner scan=new Scanner(System.in);//Scanner class
System.out.print("Enter a number: ");
int num=scan.nextInt();//get input from user
String evenOdd=(num%2==0) ? " is even":" is odd";
System.out.println(num+evenOdd+" number");
}
}

When the above code is executed, it produces the following results

case 1

Enter an integer value: 49
49 is a odd number

case 2

Enter an integer value: 100
100 is a even number

Program 3

Using Bitwise operators

import java.util.Scanner;
class OddEvenBitwise{
public static void main (String args[]){

Scanner scan=new Scanner(System.in);
System.out.print("Enter the number: ");
int num=scan.nextInt();
if((num & 1) ==1)
    System.out.print(num+ "is a odd number"); 
else 
    System.out.print(num+ "is a even number"); 
}
}

When the above code is executed, it produces the following results

case 1

Enter an integer value: 75
75 is a odd number

case 2

Enter an integer value: 32
32 is a even number

Program 4

Using Divisional operators

import java.util.Scanner;
class OddEvenJava3{
public static void main (String args[]){
Scanner scan=new Scanner(System.in);
System.out.print("Enter the number: ");
int num=scan.nextInt();

if((num/2)*2==num)
    System.out.println(num+" is even");
else
    System.out.println(num+" is odd");
}

}

 

When the above code is executed, it produces the following results

case 1

Enter an integer value: 77
77 is odd

case 2

Enter an integer value: 40
40 is even

 

Program 5

Using shift operators

import java.util.Scanner;
class OddEvenShift{
public static void main (String args[]){

Scanner scan=new Scanner(System.in);
System.out.print("Enter the number for check odd or even: ");
int num=scan.nextInt();
if(((num >> 1)<<1) ==num)
    System.out.print(num+ " is a Even number"); 
else 
    System.out.print(num+ " is a Odd number"); 
}
}

When the above code is executed, it produces the following results

case 1

Enter an integer value: 345
345 is a Odd number

case 2

Enter an integer value: 450
450 is a Even number

 

 

The same program in other languages

C++ program to check whether a number is even or odd

C program to check whether a number is even or odd

Python program to check whether a number is even or odd

 

Suggested for you

Operator in Java language

if statementsin Java

The data type in java

 

 

C program to check whether a number is even or odd
Cpp program to check whether a number 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…

2 months 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