Write a Java Program to convert binary to decimal.

import java.util.Scanner; public class MT2 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in System.out.print("Enter a binary number: "); String binaryString = scanner.nextLine( int decimalValue = 0; int length = binaryString.length() for (int i = 0; i < length; i++) { char binaryChar = binaryString.charAt(i); int binaryDigit = binaryChar - '0'; int power = length - 1 - i; decimalValue += binaryDigit * Math.pow(2, power); System.out.println("The decimal equivalent of binary " + binaryString + " is: " + decimalValue } } Copy Code
Expected Output:

Enter a binary number: 1010
The decimal equivalent of binary 1010 is: 10