How to resolve the algorithm Even or odd step by step in the Java programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Even or odd step by step in the Java programming language

Table of Contents

Problem Statement

Test whether an integer is even or odd. There is more than one way to solve this task:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Even or odd step by step in the Java programming language

The provided Java code defines five different methods named isEven, each of which takes a parameter of a different type (int, BigInteger) and returns a boolean indicating whether the number is even. Here's a detailed explanation of each method:

  1. isEven(int i): This method checks if the given integer i is even. It uses the bitwise AND operator (&) to compare the last bit of i with 1. If the result is 0, it means the number is even, and the method returns true; otherwise, it returns false. This is because in binary representation, the least significant bit of an even number is always 0, while that of an odd number is 1.

  2. isEven(int i): This is an alternative implementation that achieves the same result as the first method. It uses the modulo operator (%) to divide i by 2 and checks if the remainder is 0. If the remainder is 0, it means the number is evenly divisible by 2, indicating that it's even, and the method returns true; otherwise, it returns false.

  3. isEven(BigInteger i): This method checks if the given BigInteger object i is even. It uses the and method to perform a bitwise AND operation between i and BigInteger.ONE, which represents 1 in binary. If the result is BigInteger.ZERO, it means the last bit of i is 0, indicating that the number is even, and the method returns true; otherwise, it returns false.

  4. isEven(BigInteger i): This is a simplified implementation of the previous method. It uses the testBit method to check if the least significant bit of i is set (equal to 1). If the bit is not set, it means the number is even, and the method returns true; otherwise, it returns false.

  5. isEven(BigInteger i): This method uses the mod method to calculate the remainder when i is divided by 2 (represented by BigInteger.valueOf(2)). If the remainder is BigInteger.ZERO, it means the number is evenly divisible by 2, indicating that it's even, and the method returns true; otherwise, it returns false.

All five methods effectively determine whether a given number is even or not using different approaches and data types. The choice of which method to use depends on the specific requirements and performance considerations of the application.

Source code in the java programming language

public static boolean isEven(int i){
    return (i & 1) == 0;
}


public static boolean isEven(int i){
    return (i % 2) == 0;
}


public static boolean isEven(BigInteger i){
    return i.and(BigInteger.ONE).equals(BigInteger.ZERO);
}


public static boolean isEven(BigInteger i){
    return !i.testBit(0);
}


public static boolean isEven(BigInteger i){
    return i.mod(BigInteger.valueOf(2)).equals(BigInteger.ZERO);
}


  

You may also check:How to resolve the algorithm List comprehensions step by step in the Visual Prolog programming language
You may also check:How to resolve the algorithm Search a list step by step in the TUSCRIPT programming language
You may also check:How to resolve the algorithm Literals/Floating point step by step in the XPL0 programming language
You may also check:How to resolve the algorithm Knapsack problem/0-1 step by step in the BASIC programming language
You may also check:How to resolve the algorithm Horner's rule for polynomial evaluation step by step in the EDSAC order code programming language