Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Even Odd Program in Java

In order to identify whether a number is odd or even, Java programmers use a number of methods, which will be examined in this article. In the decimal number system, even numbers are exactly divisible by two, whereas odd numbers are not. When the modulus operator "%" is used, as in 4%3 = 1, it returns the remainder. (1 is the remainder after dividing four by three).

Even Odd Program Explanation:

A number is considered even if it may be divided evenly by 2. Odd numbers are the leftover numbers that are not exactly divisible by 2. To put it simply, odd numbers are those that have the form of n = 2k+1 whereas even numbers take the form of n = 2k. Either even or odd numbers will make up each and every integer. This blog will explain how to use a Java program to determine if a number is even or odd.

There are several methods we may use to determine if a given number is even or odd.

Let’s examine each of them separately.

Even Odd Program Algorithm:

  • Step 1- Start the program.
  • Step 2- Read/input the number.
  • Step 3- if n%2==0 then the number is even.
  • Step 4- else number is odd.
  • Step 5- display the output.
  • Step 6- Stop the program.

Odd & Even Program Pseudocode

IF (integer modulo 2) equals to 0
   PRINT number is even
ELSE
   PRINT number is odd
END IF

Java Programs to Check Odd Even Number

Let’s now study the Program to Check Even or Odd number.

Method 1: Odd-even program in Java using the modulus operator

This method displays the Java program to check the odd-even number using the modulus operator.

Code Implementation

import java.io.*;
import java.util.Scanner;
class PrepBytes 
{
	public static void main(String[] args)
	{
		int num = 10;
		if (num % 2 == 0) {
			System.out.println("Entered Number is Even");
		}
		else 
{
			System.out.println("Entered Number is Odd");
		}
	}
}

Output: Entered Number is Even

Method 2: Java program to find odd or even using a bitwise operator

We can use the bitwise AND (&) operator to check odd or even. For example, consider the binary of 7 (0111), (7 & 1 = 1). You may have noticed that every odd number’s least significant digit is 1. As a result, (odd_number & 1) is always 1 and (even_number & 1) is always 0 as well.

Code Implementation

import java.util.*;
class PrepBytes 
{
	public static void main(String[] args)
	{
		int n = 10;
		if ((n & 1) == 1) 
                {
			System.out.println("Odd");
		}
		else {
			System.out.println("Even");
		}
	}
}

Output: Even

You should now be able to use Java programming to determine whether a given integer is odd or even after running the above program. I sincerely hope you find our website to be beneficial and informative. Our discussion of the Java Odd & Even Program is now complete.

Other Java Programs

Java Program to Add Two Numbers
Java Program to Check Prime Number
Java Program to Check Whether a Number is a Palindrome or Not
Java Program to Find the Factorial of a Number
Java Program to Reverse a Number
Java Program to search an element in a Linked List
Program to convert ArrayList to LinkedList in Java
Java Program to Reverse a linked list
Java Program to search an element in a Linked List
Anagram Program in Java
Inheritance Program in Java

Leave a Reply

Your email address will not be published. Required fields are marked *