1import java.util.Scanner;
2public class BinaryToDecimalDemo
3{
4 public static void main(String[] args)
5 {
6 int number, decimal = 0, a = 0;
7 Scanner sc = new Scanner(System.in);
8 System.out.println("Please enter binary number: ");
9 String strBinary = sc.nextLine();
10 number = Integer.parseInt(strBinary);
11 while(number != 0){
12 decimal += (number % 10) * Math.pow(2, a);
13 number = number / 10;
14 a++;
15 }
16 System.out.println("Decimal number: " + decimal);
17 sc.close();
18 }
19}
1public class Details {
2
3 public int BinaryToDecimal(int binaryNumber){
4
5 int decimal = 0;
6 int p = 0;
7 while(true){
8 if(binaryNumber == 0){
9 break;
10 } else {
11 int temp = binaryNumber%10;
12 decimal += temp*Math.pow(2, p);
13 binaryNumber = binaryNumber/10;
14 p++;
15 }
16 }
17 return decimal;
18 }
19
20 public static void main(String args[]){
21 Details obj = new Details();
22 System.out.println("110 --> "+obj.BinaryToDecimal(110));
23 System.out.println("1101 --> "+obj.BinaryToDecimal(1101));
24 System.out.println("100 --> "+obj.BinaryToDecimal(100));
25 System.out.println("110111 --> "+obj.BinaryToDecimal(110111));
26 }
27}
1import java.util.Scanner;
2class BinaryToDecimal {
3 public static void main(String args[]){
4 Scanner input = new Scanner( System.in );
5 System.out.print("Enter a binary number: ");
6 String binaryString =input.nextLine();
7 System.out.println("Output: "+Integer.parseInt(binaryString,2));
8 }
9}