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:

  1. # To convert binary number to a decimal number
  2. public class BinaryDecimal {
  3. public static void main(String[] args) {
  4. long num = 11011;
  5. int decimal = convertBinaryToDecimal(num);
  6. System.out.printf("%d in binary = %d in decimal", num, decimal);
  7. }
  8. public static int convertBinaryToDecimal(long num) {
  9. int decimalNumber = 0, i = 0;
  10. long remainder;
  11. while (num != 0) {
  12. remainder = num % 10;
  13. num /= 10;
  14. decimalNumber += remainder * Math.pow(2, i);
  15. ++i;
  16. }
  17. return decimalNumber;
  18. }
  19. }

Output:

11011 in binary = 27 in decimal