What is odd and even in java

Java Program to Check Whether a Number is Even or Odd

To understand this example, you should have the knowledge of the following Java programming topics:

Example 1: Check whether a number is even or odd using if. else statement

import java.util.Scanner; public class EvenOdd < public static void main(String[] args) < Scanner reader = new Scanner(System.in); System.out.print("Enter a number: "); int num = reader.nextInt(); if(num % 2 == 0) System.out.println(num + " is even"); else System.out.println(num + " is odd"); >>
Enter a number: 12 12 is even

In the above program, a Scanner object, reader is created to read a number from the user’s keyboard. The entered number is then stored in a variable num .

Читайте также:  With open as file python no such file or directory

Now, to check whether num is even or odd, we calculate its remainder using % operator and check if it is divisible by 2 or not.

For this, we use if. else statement in Java. If num is divisible by 2 , we print num is even. Else, we print num is odd.

We can also check if num is even or odd by using ternary operator in Java.

Example 2: Check whether a number is even or odd using ternary operator

import java.util.Scanner; public class EvenOdd < public static void main(String[] args) < Scanner reader = new Scanner(System.in); System.out.print("Enter a number: "); int num = reader.nextInt(); String evenOdd = (num % 2 == 0) ? "even" : "odd"; System.out.println(num + " is " + evenOdd); >>
Enter a number: 13 13 is odd

In the above program, we’ve replaced if. else statement with ternary operator (? 🙂 .

Here, if num is divisible by 2, «even» is returned. Else, «odd» is returned. The returned value is saved in a string variable evenOdd .

Then, the result is printed on the screen using string concatenation.

Источник

Java Program to Check If a Number is Even or Odd

Java Course - Mastering the Fundamentals

Mathematics classifies numbers into different categories. Odd and even numbers are one of these categorical divisions. Numbers divisible by 2 are called Even numbers , and numbers not divisible by 2 are called Odd numbers . Even numbers end with 0,2,4,6,8 , while odd numbers end with 1,3,5,7,9 . There are several approaches to determining if a number is even or odd using Java programming language.

These approaches range from using simple modulo operators to employing bitwise operators to determine if a number is even or not. We will discuss each of these approaches with examples.

Introduction

Even numbers are numbers that leave the remainder 0 when divided by 2 . All the numbers(either positive or negative) that are divisible by 2 i.e. ends with 0,2,4,6, and 8 are known as even numbers .

A number is divisible by 2 also means when a number is divided by 2 the resulting quotient is a whole number and not a fraction. 0 will be an even number since when divided by 2 it will result in a whole number, i.e. 0 . Furthermore, the remainder of 0 / 2 is 0 as 2 * 0 = 0 .

On the other hand, numbers that leave a remainder 1 when divided by 2 are known as Odd numbers . All the numbers (either positive or negative) that end with 1,3,5,7, and 9 are known as odd numbers.

Methods to Check If a Number is Even or Odd in Java

Different approaches can be followed while programming to determine even-odd numbers . Let’s see those approaches in this section starting from the most simple brute force approach .

Java Program to Check If a Number is Even or Odd Using Brute Force — Naive Approach

The Brute Force approach is the most simple approach to designing a program for even-odd numbers. The brute force approach follows straight forward method using conditional statements. It checks the remainder after the division of number with 2 . If the remainder is 0 then it is an even number else it is an odd number .

In the above program, number % 2 == 0 statement checks if the remainder of division is 0 . If the statement is satisfied then the number is even else it is odd. 987 % 2 == 1 hence 987 is an odd number.

Java Program to Check If a Number is Even or Odd Using Bitwise Operators

Bitwise operators OR, AND, and XOR can be used to classify numbers as even-odd depending on the result of bitwise operations performed on the binary numbers.

1. Using Bitwise OR

Bitwise OR (|): The bitwise OR operator denoted by | compares the binary values of operands with each other and yields a value of 1 if either of the operands has bit 1 . If both of the bits are 0 , the result of that bit is 0 , or else the result is 1 .

The following table shows the results of the Bitwise OR operators:

Bitwise OR for Even-Odd operations:

  • Bitwise OR operation of an even number(in binary form) by 1, increment the value of the number by 1 .
  • Bitwise OR operation of an odd number(in binary form) by 1 does not change the value of the number.

Example: Let’s check if a number is even or odd using Bitwise OR.

BitwiseOR Even Example

Result: 16 is an even number hence, bitwise OR by 1 increment the number by 1 and yields 17 .

BitwiseOR Odd example

Result: 19 is an odd number hence, bitwise OR by 1 doesn’t increment the value and yields 19 as it is.

Program:
Let’s see an implementation of the even-odd program using the Bitwise OR operator using java code .

In the above example, (number | 1) > number performs a Bitwise OR operation on number by 1 and checks if the result is greater than the number itself. The number is even when the if the condition is satisfied as bitwise OR on even number by 1 increment the number 1 else the number is odd.

2. Using Bitwise AND

Bitwise AND (&): The bitwise AND operator denoted by & compares the binary values of operands with each other and yields a value of 1 if both of the operands have bit 1 . If both of the bits are 1, the result of that bit is 1 , else the result is 0 .

The following table shows the results of the Bitwise AND operators.

Bitwise AND for Even-Odd operations:

  • Bitwise AND operation of the even number(in binary form) by 1 will yield 0 .
  • Bitwise AND operation of the odd number(in binary form) by 1 will yield 1 .

Example: Let’s check if a number is even or odd using Bitwise AND.

BitwiseAND Even example

Result: 6 is an even number hence, bitwise AND by 1 yields 0 .

BitwiseAND Odd example

Result: 17 is an odd number hence, bitwise AND by 1 yields 1 .

Program: Let’s see an implementation of the even-odd program using the Bitwise AND operator using java code.

In the above example, (number & 1) == 0 performs a bitwise AND operation on number by 1 and checks if the result is equal to 0 . The number is even when the if the condition is satisfied as bitwise AND on an even number by 1 yields 0 while bitwise AND on an odd number by 1 yields 1 .

3. Using Bitwise XOR

Bitwise XOR (^): The bitwise XOR operator denoted by ^ compares the binary values of operands with each other and yields a value of 1 if both of the operands have different bits. XOR yields a value of 0 if both operands have same bits.

The following table shows the results of the Bitwise XOR operators.

Bitwise XOR for Even-Odd operations:

  1. Bitwise XOR operation of the even number(in binary form) by 1 increments the value of the number by 1 .
  2. Bitwise XOR operation of the odd number(in binary form) by 1 decrement the value of the number by 1 .

The XOR operation is the most efficient way to determine whether a number is even or odd. A binary XOR of a number by one increment that number by 1 if it is an even number, and decrement that number by 1 if it is an odd number.

Example: Let’s check if a number is even or odd using Bitwise XOR.

BitwiseXOR Even example

Result: 12 is an even number hence, bitwise XOR by 1 increments the number by 1 and yields 13 .

BitwiseXOR Odd example

Result- 3 is an even number hence, bitwise XOR by 1 decrement the number by 1 and yields 2 .

Program: Let’s see an implementation of the even-odd program using the Bitwise XOR operator using java code .

In the above example, (number ^ 1) == number + 1 performs a Bitwise XOR operation on the number by 1 and checks if the result is greater than 1 by the number itself. The number is even when if the condition is satisfied as bitwise XOR on an even number by 1 increment the number by 1 and bitwise XOR on an odd number by 1 decrement the number by 1 .

Java Program to Check If a Number is Even or Odd by Checking the Least Significant Bit

Binary representation of 3: 11 . The last bit in binary form is the Least Significant Bit(LSB) of that number. In the case of 3 LSB is 1 since 11.

In the above program, Integer.toBinaryString(a).endsWith(«0») converts integer to binary string and checks if LSB is 0 i.e. it ends with 0. If the statement is true then the integer i is even else it is odd.

Java Program to Check If a Number is Even or Odd Using a Ternary Operator

The ternary operator in Java denoted by ? is used to evaluate a conditional expression. It is the only conditional operator in Java that takes 3 operands.

The ternary operator evaluates the condition , if it is true expression1 is stored in the variable and if the condition is false expression2 is stored in the variable.

In the above program, the ternary operator ? is used instead of the if-else statement. String check is used to evaluate the expression using the ternary operator and save the result. If i is divisible by 2 , even is returned else odd is returned.

Conclusion

  • Even Odd program in Java can be achieved with different approaches like the Brute Force approach, using Bitwise operators, using LSB, and using ternary operator.
  • An even number is divisible by 2 and leaves the remainder 0 when divided by 2 .
  • An odd number is not divisible by 2 and leaves the remainder 1 when divided by 2 .
  • Performing OR operation between an even number and 1 results in incrementing it by 1. However, the OR operation between an odd number and 1 decreases the value of the odd number by 1.
  • Performing AND operation between an even number and 1 yields 0. However, AND operation between an odd number and 1 yields 1.
  • XOR operation between an even number and 1 increment the value of the even number by 1. However, with an odd number, the same operation decrements its value.
  • Most optimal approach to determine even odd numbers is using the bitwise XOR operator .
  • Ternary Operator can be used in place of conventional if-else statements to implement the naive logic.
  • We can also use the Least Significant Bit of the given number to determine if it is even or odd . If the LSB is 0, the number is even otherwise odd.

See More

Источник

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"); >> >

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

Источник

Оцените статью