Site icon Codeforcoding

Java code to check whether a number is even or odd

Java program to check whether a number is even or odd

Java program to check 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

 

 

Similar post

Java program to find a number is even or odd using the method

Java program to separate Odd and Even numbers from an array

Java program to display all even and odd numbers from 1 to n

Java program display odd and even numbers without if statements

Java program to calculate the sum of odd and even numbers

Java program to find whether a number is even or odd

 

 

Suggested for you

Operator in Java language

if statements in 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
Exit mobile version