# Java Program to Convert Binary to Decimal

Posted March 16, 2016

on:**Objectives :**

- Binary to Decimal Conversion
- Converting Binary to Decimal
- Write a program to convert binary to decimal
- Write a program to convert binary number to decimal format
- Write a program to convert binary to decimal using Scanner class
- Write a program to convert binary to decimal, take input using Scanner class

**Java Program :**

import java.util.Scanner; public class BinaryToDecimal { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter binary number: "); String binary = scanner.nextLine(); int decimal = binaryToDecimal(binary); System.out.println("Decimal equivalent of "+ binary +" is "+ decimal); } private static int binaryToDecimal(String binary) { final int base = 2; int decimal = 0; for (int i = 0; i < binary.length(); i++) { if (binary.charAt(i) == '0') { decimal += 0 * Math.pow(base, binary.length() - i - 1); } else if (binary.charAt(i) == '1') { decimal += 1 * Math.pow(base, binary.length() - i - 1); } else { System.out.println("Invalid Binary Number"); System.out.println("Binary Number Contains only 0's or 1's"); System.exit(0); } } return decimal; } }

**Output :**

Enter binary number: 1110

Decimal equivalent of 1110 is 14

Advertisements

Tags: Best Java Trainer in Pune, Best Java Training in Pune, binary to decimal conversion, Converting Binary to Decimal, java program, Java Program for Beginners, Java Program for Scanner class, Java Program to Convert Binary format to Decimal number, Java Program to Convert Binary to Decimal, Java Programming, Java Training, Java Training in Pune, Java Training Videos, take input using Scanner class, Write a program to convert binary number to decimal format, Write a program to convert binary to decimal, Write a program to convert binary to decimal using Scanner class

## Leave a Reply