Java

1) Class Declaration: The code starts with the declaration of a Java class named BinaryToDecimal.
2) Main Method: Inside the class, there's a main method which serves as the entry point for the Java application.
3) Binary String: A string variable named binary is declared and initialized with the binary value "110011". This represents a binary number that we want to convert to decimal.
4) Conversion to Decimal: The parseInt method of the Integer class is used to convert the binary string (binary) to its equivalent decimal representation. The second argument 2 specifies that the input string is in base-2 (binary). The result of this conversion is stored in an integer variable i.
5) Output: Finally, the code prints out both the binary and decimal representations of the number using System.out.println. It prints "Binary:110011 || Decimal:51".

public class BinaryToDecimal {
    public static void main(String[] args) {
       
        String binary="110011";

        int i=Integer.parseInt(binary, 2);

        System.out.println("Binary:"+binary+" || Decimal:"+i);

    }
}
Conclusion:
In summary, this Java program demonstrates how to convert a binary number represented as a string into its decimal equivalent. It utilizes the parseInt method from the Integer class, specifying base 2 as the input format for binary numbers. The conversion is then printed out for verification. This code is helpful for developers who need to work with binary and decimal number representations in Java applications.

Related Posts

Table Of Contents

;