import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class BinaryToDecimal { public static void main(String args[]) throws Exception, IOException { int bin; int i = 0; int ldigit=0; int dcm=0; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter Binary value: "); bin=Integer.parseInt(br.readLine()); while (bin != 0) { ldigit = bin%10; //last digit of binary no dcm = dcm+ldigit *((int) Math.pow(2,i)); i++; bin = bin / 10; //removimg last digit from binary no } System.out.println("The decimal value for the given binary is: "+dcm); } }
OUTPUT:
Enter Binary value: 1101 The decimal value for the given binary is:13