w3resource

Java: Convert a binary number to decimal number

Java Basic: Exercise-22 with Solution

Write a Java program to convert a binary number to a decimal number.

Binary number: A binary number is a number expressed in the base-2 numeral system or binary numeral system. This system uses only two symbols: typically 1 (one) and 0 (zero).

Decimal number: The decimal numeral system is the standard system for denoting integer and non-integer numbers. It is also called base-ten positional numeral system.

Pictorial Presentation: Binary to Decimal number

Java: Convert a binary number to decimal number

Sample Solution:

Java Code:

import java.util.Scanner;

public class Exercise22 {
    public static void main(String[] args) {
        // Create a Scanner object to read input from the user
        Scanner sc = new Scanner(System.in);
        
        // Declare variables to store binary and decimal numbers, remainder, and a multiplier
        long binaryNumber, decimalNumber = 0, j = 1, remainder;
        
        // Prompt the user to input a binary number
        System.out.print("Input a binary number: ");
        binaryNumber = sc.nextLong();

        // Convert the binary number to decimal
        while (binaryNumber != 0) {
            remainder = binaryNumber % 10;
            decimalNumber = decimalNumber + remainder * j;
            j = j * 2;
            binaryNumber = binaryNumber / 10;
        }
        
        // Display the decimal representation of the binary number
        System.out.println("Decimal Number: " + decimalNumber);
    }
}

Explanation:

In the exercise above -

  • It takes a binary number as input from the user using the "Scanner" class and stores it in the variable 'binaryNumber'.
  • It initializes a variable 'decimalNumber' to store the decimal equivalent and another variable 'j' as a multiplier, starting from 1.
  • It enters a loop to perform the binary-to-decimal conversion:
    • In each iteration, it calculates the remainder when the 'binaryNumber' is divided by 10 (which gives the least significant binary digit).
    • It then adds this remainder multiplied by the current value of 'j' to the 'decimalNumber'.
    • It multiplies 'j' by 2 to prepare for the next binary digit.
    • It updates 'binaryNumber' by removing the least significant digit (rightmost) and by dividing it by 10.
    • The loop continues until 'binaryNumber' becomes zero, effectively converting the entire binary number to decimal.
  • After the loop, it prints the decimal representation of the binary number stored in the 'decimalNumber' variable.

Sample Output:

Input a binary number: 100                                                                                    
Decimal Number: 4 

Flowchart:

Flowchart: Java exercises: Convert a binary number to decimal number

Java Code Editor:

Previous: Write a Java program to convert a decimal number to octal number.
Next: Write a Java program to convert a binary number to hexadecimal number.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/java-exercises/basic/java-basic-exercise-22.php