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 DecimalToBinaryExample2{
2public static void toBinary(int decimal){
3 int binary[] = new int[40];
4 int index = 0;
5 while(decimal > 0){
6 binary[index++] = decimal%2;
7 decimal = decimal/2;
8 }
9 for(int i = index-1;i >= 0;i--){
10 System.out.print(binary[i]);
11 }
12System.out.println();//new line
13}
14public static void main(String args[]){
15System.out.println("Decimal of 10 is: ");
16toBinary(10);
17System.out.println("Decimal of 21 is: ");
18toBinary(21);
19System.out.println("Decimal of 31 is: ");
20toBinary(31);
21}}
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}