Introduction
Converting a binary number to its decimal equivalent is a fundamental task in computing. Binary numbers are base-2 numbers, consisting only of 0s and 1s, while decimal numbers are base-10 numbers, which are commonly used in everyday arithmetic. This guide will walk you through writing a Java program that converts a given binary number to its decimal equivalent.
Problem Statement
Create a Java program that:
- Prompts the user to enter a binary number.
- Converts the binary number to its decimal equivalent.
- Displays the decimal equivalent.
Example:
-
Input:
"1010"
-
Output:
"Decimal of 1010 is 10"
-
Input:
"1111"
-
Output:
"Decimal of 1111 is 15"
Solution Steps
- Read the Binary Number: Use the
Scanner
class to take the binary number as input from the user. - Convert Binary to Decimal: Use either the built-in method
Integer.parseInt()
or manually convert the binary number using a loop. - Display the Result: Print the decimal equivalent.
Java Program Using Built-in Method
// Java Program to Convert Binary to Decimal using Integer.parseInt() // Author: https://www.rameshfadatare.com/ import java.util.Scanner; public class BinaryToDecimal { public static void main(String[] args) { // Step 1: Read the binary number from the user try (Scanner scanner = new Scanner(System.in)) { System.out.print("Enter a binary number: "); String binary = scanner.nextLine(); // Step 2: Convert the binary number to decimal using built-in method int decimal = Integer.parseInt(binary, 2); // Step 3: Display the result System.out.println("Decimal of " + binary + " is " + decimal); } } }
Java Program Using a Manual Conversion Method
// Java Program to Convert Binary to Decimal without using Integer.parseInt() // Author: https://www.rameshfadatare.com/ import java.util.Scanner; public class BinaryToDecimalManual { public static void main(String[] args) { // Step 1: Read the binary number from the user try (Scanner scanner = new Scanner(System.in)) { System.out.print("Enter a binary number: "); String binary = scanner.nextLine(); // Step 2: Convert the binary number to decimal manually int decimal = 0; int power = 0; for (int i = binary.length() - 1; i >= 0; i--) { int digit = binary.charAt(i) - '0'; // Convert char to int decimal += digit * Math.pow(2, power); power++; } // Step 3: Display the result System.out.println("Decimal of " + binary + " is " + decimal); } } }
Explanation
Step 1: Read the Binary Number
- The
Scanner
class is used to read a binary number input from the user as a string. ThenextLine()
method captures the input.
Step 2: Convert Binary to Decimal
- Using Built-in Method:
- The
Integer.parseInt(binary, 2)
method converts a binary string directly to its decimal equivalent. The second argument,2
, specifies the base of the input number.
- The
- Manual Conversion:
- The program iterates through each digit of the binary number string, starting from the least significant bit (rightmost digit).
- For each digit, it multiplies the digit by 2 raised to the power of its position (starting from 0) and adds the result to the cumulative decimal value.
Step 3: Display the Result
- The program prints the decimal equivalent of the binary number using
System.out.println()
.
Output Example
Example 1: Using Built-in Method
Enter a binary number: 1010 Decimal of 1010 is 10
Example 2: Using Manual Conversion
Enter a binary number: 1111 Decimal of 1111 is 15
Example 3: Handling a Larger Binary Number
Enter a binary number: 100110 Decimal of 100110 is 38
Conclusion
These Java programs demonstrate how to convert a binary number to its decimal equivalent using both a built-in method and a manual conversion process. The solutions provided cover basic concepts such as loops, arithmetic operations, and string manipulation, making them valuable exercises for beginners learning Java programming.