Check if a N base number is Even or Odd in Java
In computer science, understanding the parity (even or odd) of numbers represented in different base systems is crucial for various applications. Given a number represented in a specific base, the task is to develop a Java program that can accurately determine whether the number is even or odd· It involves converting the base-N number to its decimal equivalent and then checking its parity· The goal is to create an efficient and reliable solution that works for numbers in any base system provided by the user.
N Base:
In mathematics and computer science, a number system is a way of representing numbers using digits and a base· The most common number system is the decimal system, which is base-10, meaning it uses 10 digits (0 through 9)·
An "N base" number system refers to any number system with a base of N, where N can be any positive integer greater than 1· For example, binary (base-2), octal (base-8), and hexadecimal (base-16) are all different number systems with their own set of digits and rules for representing numbers·
Even:
In mathematics, an even number is any integer that is divisible by 2 without leaving a remainder· In other words, if you divide an even number by 2, the result is an integer·
For example, 2, 4, 6, 8, and 10 are all even numbers because they can be divided evenly by 2·
Odd:
Conversely, an odd number is any integer that is not divisible by 2 without leaving a remainder· If you divide an odd number by 2, the result is not an integer·
For example, 1, 3, 5, 7, and 9 are all odd numbers because they cannot be divided evenly by 2·
Examples:
Example 1:
Input:
Number: 1011 Base: 2 (Binary)
Output:
The number is odd·
Explanation:
Convert the binary number 1011 to its decimal equivalent: (1 * 2^3) + (0 * 2^2) + (1 * 2^1) + (1 * 2^0) = 11·
11 is an odd number, so the output is "The number is odd·"
Example 2:
Input:
Number: 25 Base: 8 (Octal)
Output:
The number is odd·
Explanation:
Convert the octal number 25 to its decimal equivalent: (2 * 8^1) + (5 * 8^0) = 21·
21 is an odd number, so the output is "The number is odd·"
Example 3:
Input:
Number: 3A Base: 16 (Hexadecimal)
Output:
The number is odd·
Explanation:
Convert the hexadecimal number 3A to its decimal equivalent: (3 * 16^1) + (10 * 16^0) = 58·
58 is an even number, so the output is "The number is even."
Approach 1: Normal (Basic) Method
ALGORITHM:
Step 1: Prompt the user to input the number represented in base-N and the base itself·
Step 2: Iterate through each digit of the number from right to left·
Step 3: For each digit, determine its value in decimal based on its position and the base·
Step 4: Accumulate the decimal value by multiplying each digit's value with the appropriate power of the base·
Step 5: After converting the number to decimal, check if the resulting decimal number is even or odd·
Step 6: Determine this by performing the modulo operation (%) with 2 on the decimal number·
Step 7: If the remainder is 0, the number is even; otherwise, it's odd·
Step 8: Print the original number, its decimal equivalent, and whether it's even or odd·
Implementation:
The implementation of the above steps given below.
FileName: EvenOddBaseN.java
import java.util.Scanner; public class EvenOddBaseN { // Function to convert a number from base-N to decimal public static int baseNToDecimal(String numBaseN, int base) { int decimal = 0; int power = 0; for (int i = numBaseN.length() - 1; i >= 0; i--) { int digit = Character.digit(numBaseN.charAt(i), base); decimal += digit * Math.pow(base, power); power++; } return decimal; } // Function to check if a number is even or odd public static String checkEvenOdd(int decimal) { if (decimal % 2 == 0) { return "even"; } else { return "odd"; } } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // Example 1 System.out.println("Example 1:"); String numBaseN1 = "1011"; int base1 = 2; int decimal1 = baseNToDecimal(numBaseN1, base1); System.out.println("Number: " + numBaseN1 + " (Base " + base1 + ")"); System.out.println("Decimal Equivalent: " + decimal1); System.out.println("The number is " + checkEvenOdd(decimal1) + "."); System.out.println(); // Example 2 System.out.println("Example 2:"); String numBaseN2 = "25"; int base2 = 8; int decimal2 = baseNToDecimal(numBaseN2, base2); System.out.println("Number: " + numBaseN2 + " (Base " + base2 + ")"); System.out.println("Decimal Equivalent: " + decimal2); System.out.println("The number is " + checkEvenOdd(decimal2) + "."); System.out.println(); // Example 3 System.out.println("Example 3:"); String numBaseN3 = "3A"; int base3 = 16; int decimal3 = baseNToDecimal(numBaseN3, base3); System.out.println("Number: " + numBaseN3 + " (Base " + base3 + ")"); System.out.println("Decimal Equivalent: " + decimal3); System.out.println("The number is " + checkEvenOdd(decimal3) + "."); scanner.close(); } }
Output:
Example 1: Number: 1011 (Base 2) Decimal Equivalent: 11 The number is odd. Example 2: Number: 25 (Base 8) Decimal Equivalent: 21 The number is odd. Example 3: Number: 3A (Base 16) Decimal Equivalent: 58 The number is even.
Complexity Analysis:
Time Complexity:
Converting a number from base-N to its decimal equivalent involves iterating through each digit of the number, which takes linear time relative to the number of digits·
Thus, the time complexity is O(n), where n is the number of digits in the input number·
Space Complexity:
The space complexity is O(1) because the algorithm uses a constant amount of extra space regardless of the size of the input·
Approach 2: Conversion to Decimal
ALGORITHM:
Step 1: Prompt the user to enter the number represented in base-N and specify the base (e·g·, binary, octal, hexadecimal)·
Step 2: Start by initializing the decimal equivalent to 0·
Step 3: Iterate through each digit of the input number from right to left·
Step 4: For each digit, determine its value in decimal·
Step 5: If the digit is a numeric character ('0' to '9'), subtract the ASCII value of '0' to get its integer value·
Step 6: If the digit is an alphabetic character ('A' to 'F' for hexadecimal representation), subtract the ASCII value of 'A' and add 10 to get its integer value·
Step 7: Multiply the integer value of each digit by the appropriate power of the base (base^position) and add it to the decimal equivalent·
Step 8: Increment the power for each position·
Step 9: After converting the number to decimal, determine its parity (even or odd).
Step 10: Check if the decimal equivalent is divisible by 2 without a remainder·
Step 10.1: If yes, the number is even·
Step 10.2: If no, the number is odd·
Step 11: Display the original number, its decimal equivalent, and whether it's even or odd·
Implementation:
The implementation of the above steps given below
FileName: EvenOddBaseN.java
import java.util.Scanner; public class EvenOddBaseN { // Function to convert a number from base-N to decimal public static int baseNToDecimal(String numBaseN, int base) { int decimal = 0; int power = 0; for (int i = numBaseN.length() - 1; i >= 0; i--) { char digitChar = numBaseN.charAt(i); int digitValue; if (Character.isDigit(digitChar)) { digitValue = digitChar - '0'; // Convert digit character to integer value } else { digitValue = digitChar - 'A' + 10; // Convert hexadecimal digit character to integer value } decimal += digitValue * Math.pow(base, power); power++; } return decimal; } // Function to check if a number is even or odd public static String checkEvenOdd(int decimal) { if (decimal % 2 == 0) { return "even"; } else { return "odd"; } } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // Example 1 System.out.println("Example 1:"); String numBaseN1 = "1011"; int base1 = 2; int decimal1 = baseNToDecimal(numBaseN1, base1); System.out.println("Number: " + numBaseN1 + " (Base " + base1 + ")"); System.out.println("Decimal Equivalent: " + decimal1); System.out.println("The number is " + checkEvenOdd(decimal1) + "."); System.out.println(); // Example 2 System.out.println("Example 2:"); String numBaseN2 = "25"; int base2 = 8; int decimal2 = baseNToDecimal(numBaseN2, base2); System.out.println("Number: " + numBaseN2 + " (Base " + base2 + ")"); System.out.println("Decimal Equivalent: " + decimal2); System.out.println("The number is " + checkEvenOdd(decimal2) + "."); System.out.println(); // Example 3 System.out.println("Example 3:"); String numBaseN3 = "3A"; int base3 = 16; int decimal3 = baseNToDecimal(numBaseN3, base3); System.out.println("Number: " + numBaseN3 + " (Base " + base3 + ")"); System.out.println("Decimal Equivalent: " + decimal3); System.out.println("The number is " + checkEvenOdd(decimal3) + "."); scanner.close(); } }
Output:
Example 1: Number: 1011 (Base 2) Decimal Equivalent: 11 The number is odd. Example 2: Number: 25 (Base 8) Decimal Equivalent: 21 The number is odd. Example 3: Number: 3A (Base 16) Decimal Equivalent: 58 The number is even.
Complexity Analysis:
Time Complexity:
The time complexity of the algorithm is O(n), where n is the number of digits in the input number· It is because the algorithm iterates through each digit of the input number once to convert it to its decimal equivalent·
Space Complexity:
The space complexity of the algorithm is O(1), indicating constant space usage· It is because the algorithm only uses a fixed amount of extra space regardless of the size of the input number.
Approach 3: Using Recursion
Step 1: Prompt the user to input the number represented in base-N and specify the base (e·g·, binary, octal, hexadecimal)·
ALGORITHM:
Step 2: Define a recursive function baseNToDecimal that takes three parameters: the number represented in base-N, the base, and the current index (initially set to the index of the last digit)·
Step 2.1: Base case: If the index becomes less than 0, return 0·
Step 2.2: Recursive case: Extract the digit at the current index of the number·
Step 3: If the digit is numeric ('0' to '9'), convert it to its integer value by subtracting the ASCII value of '0'·
Step 4: If the digit is alphabetic ('A' to 'F' for hexadecimal representation), convert it to its integer value by subtracting the ASCII value of 'A' and adding 10·
Step 5: Multiply the digit value by the appropriate power of the base (base^position) and recursively call the function with the updated index·
Step 6: Accumulate the results of the recursive calls·
Step 7: After converting the number to decimal, determine its parity (even or odd)·
Step 8: Check if the decimal equivalent is divisible by 2 without a remainder·
Step 8.1: If yes, the number is even·
Step 8.2: If no, the number is odd·
Step 9: Display the original number, its decimal equivalent, and whether it's even or odd·
Implementation:
The Implementation of the above steps given below
FileName: EvenOddBaseN.java
import java.util.Scanner; public class EvenOddBaseN { // Function to convert a number from base-N to decimal recursively public static int baseNToDecimal(String numBaseN, int base, int index) { if (index < 0) { return 0; } char digitChar = numBaseN.charAt(index); int digitValue; if (Character.isDigit(digitChar)) { digitValue = digitChar - '0'; // Convert digit character to integer value } else { digitValue = digitChar - 'A' + 10; // Convert hexadecimal digit character to integer value } return digitValue * (int)Math.pow(base, numBaseN.length() - 1 - index) + baseNToDecimal(numBaseN, base, index - 1); } // Function to check if a number is even or odd public static String checkEvenOdd(int decimal) { if (decimal % 2 == 0) { return "even"; } else { return "odd"; } } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // Example 1 System.out.println("Example 1:"); String numBaseN1 = "1011"; int base1 = 2; int decimal1 = baseNToDecimal(numBaseN1, base1, numBaseN1.length() - 1); System.out.println("Number: " + numBaseN1 + " (Base " + base1 + ")"); System.out.println("Decimal Equivalent: " + decimal1); System.out.println("The number is " + checkEvenOdd(decimal1) + "."); System.out.println(); // Example 2 System.out.println("Example 2:"); String numBaseN2 = "25"; int base2 = 8; int decimal2 = baseNToDecimal(numBaseN2, base2, numBaseN2.length() - 1); System.out.println("Number: " + numBaseN2 + " (Base " + base2 + ")"); System.out.println("Decimal Equivalent: " + decimal2); System.out.println("The number is " + checkEvenOdd(decimal2) + "."); System.out.println(); // Example 3 System.out.println("Example 3:"); String numBaseN3 = "3A"; int base3 = 16; int decimal3 = baseNToDecimal(numBaseN3, base3, numBaseN3.length() - 1); System.out.println("Number: " + numBaseN3 + " (Base " + base3 + ")"); System.out.println("Decimal Equivalent: " + decimal3); System.out.println("The number is " + checkEvenOdd(decimal3) + "."); scanner.close(); } }
Output:
Example 1: Number: 1011 (Base 2) Decimal Equivalent: 11 The number is odd. Example 2: Number: 25 (Base 8) Decimal Equivalent: 21 The number is odd. Example 3: Number: 3A (Base 16) Decimal Equivalent: 58 The number is even.
Complexity Analysis:
Time Complexity:
The time complexity of the algorithm for converting a number from base-N to decimal using recursion is O(n), where n is the number of digits in the input number. It is because the algorithm iterates through each digit of the input number once in the recursive calls.
Space Complexity:
The space complexity of the algorithm is O(n), where n is the number of recursive calls made. It is because each recursive call consumes space on the call stack proportional to the depth of the recursion, which is determined by the number of digits in the input number.
Approach 4: Using Bitwise Operation
ALGORITHM:
Step 1: Start by creating a scanner object to read input from the user. It will allow us to input numbers represented in different base systems.
Step 2: Define a function checkEvenOdd that takes two parameters: numBaseN (the number represented in base-N) and base (the base of the number).
Step 3: Inside the checkEvenOdd function, convert the input number numBaseN from its given base to its decimal equivalent using Integer.parseInt(numBaseN, base). The step is crucial as it allows us to work with the number in a common base (decimal) for parity checking.
Step 4: After converting the number to its decimal equivalent, check if the decimal value is even or odd. It can be done using a bitwise AND operation with 1 (decimalValue & 1). If the result is 0, the number is even; otherwise, it's odd.
Step 5: Return "even" or "odd" based on the parity check result.
Step 6: In the main function, provide examples of numbers represented in different base systems (binary, octal, and hexadecimal). Print each example along with its base and the result of the checkEvenOdd function.
Implementation:
The implementation of the above steps given below
FileName: EvenOddBaseN.java
import java.util.Scanner; public class EvenOddBaseN { // Function to check if a number is even or odd using bitwise AND public static String checkEvenOdd(String numBaseN, int base) { // Convert the number to its decimal equivalent int decimalValue = Integer.parseInt(numBaseN, base); // Check if the decimal equivalent is even or odd if ((decimalValue & 1) == 0) { return "even"; } else { return "odd"; } } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // Example 1 System.out.println("Example 1:"); String numBaseN1 = "1011"; System.out.println("Number: " + numBaseN1 + " (Binary)"); System.out.println("The number is " + checkEvenOdd(numBaseN1, 2) + "."); System.out.println(); // Example 2 System.out.println("Example 2:"); String numBaseN2 = "25"; System.out.println("Number: " + numBaseN2 + " (Octal)"); System.out.println("The number is " + checkEvenOdd(numBaseN2, 8) + "."); System.out.println(); // Example 3 System.out.println("Example 3:"); String numBaseN3 = "3A"; System.out.println("Number: " + numBaseN3 + " (Hexadecimal)"); System.out.println("The number is " + checkEvenOdd(numBaseN3, 16) + "."); scanner.close(); } }
Output:
Example 1: Number: 1011 (Binary) The number is odd. Example 2: Number: 25 (Octal) The number is odd. Example 3: Number: 3A (Hexadecimal) The number is even.
Complexity Analysis:
Time Complexity:
The time complexity of the checkEvenOdd function is O(1) because it involves basic arithmetic operations and a bitwise AND operation, which have constant time complexity.
Space Complexity:
The space complexity is also O(1) as it only requires a constant amount of space for variables and temporary storage.