Java Program to Convert Binary to Decimal
In this example, we are giving a program to convert binary number to a decimal number.
To convert binary to decimal:
# To convert binary number to a decimal number
public class BinaryDecimal {
public static void main(String[] args) {
long num = 11011;
int decimal = convertBinaryToDecimal(num);
System.out.printf("%d in binary = %d in decimal", num, decimal);
}
public static int convertBinaryToDecimal(long num) {
int decimalNumber = 0, i = 0;
long remainder;
while (num != 0) {
remainder = num % 10;
num /= 10;
decimalNumber += remainder * Math.pow(2, i);
++i;
}
return decimalNumber;
}
}
Output:
11011 in binary = 27 in decimal