Write a Java program to convert binary to decimal. We can use the parseInt with two as the second argument will convert the binary string to a decimal integer.
package Remaining;
public class BinaryToDecimal1 {
public static void main(String[] args) {
String s1 = "1101";
String s2 = "10101";
String s3 = "11111";
String s4 = "110101";
System.out.println(Integer.parseInt(s1, 2));
System.out.println(Integer.parseInt(s2, 2));
System.out.println(Integer.parseInt(s3, 2));
System.out.println(Integer.parseInt(s4, 2));
}
}
13
21
31
53
This example accepts the binary string and converts it to a decimal number.
package Remaining;
import java.util.Scanner;
public class BinaryToDecimal2 {
private static Scanner sc;
public static void main(String[] args) {
sc= new Scanner(System.in);
System.out.print("Please Enter Binary Number = ");
String binaryString = sc.nextLine();
int decimalVal = Integer.parseInt(binaryString, 2);
System.out.println("Binary To Decimal Result = " + decimalVal);
}
}

This program helps to convert binary to a decimal using a while loop. We have already explained the logic in c programs.
package Remaining;
import java.util.Scanner;
public class BinaryToDecimal3 {
private static Scanner sc;
public static void main(String[] args) {
int binaryVal, temp, remainder, decimal = 0, n = 0;
sc= new Scanner(System.in);
System.out.print("Please Enter Number = ");
binaryVal = sc.nextInt();
temp = binaryVal;
while(temp > 0)
{
remainder = temp % 10;
decimal = (int) (decimal + remainder * Math.pow(2, n));
temp = temp / 10;
n++;
}
System.out.println("Binary " + binaryVal + " To Decimal Result = " + decimal);
}
}
Please Enter Number = 110011001
Binary 110011001 To Decimal Result = 409
Please Enter Number = 11111
Binary 11111 To Decimal Result = 31
The below program accepts the number and converts the binary value to a decimal number using functions.
package Remaining;
import java.util.Scanner;
public class BinaryToDecimal4 {
private static Scanner sc;
public static void main(String[] args) {
sc= new Scanner(System.in);
System.out.print("Please Enter Number = ");
int binaryVal = sc.nextInt();
int decimal = binaryToDecimal(binaryVal);
System.out.println("Result = " + decimal);
}
public static int binaryToDecimal(int binaryVal)
{
int remainder, decimal = 0, base = 1;
while(binaryVal > 0)
{
remainder = binaryVal % 10;
decimal = decimal + (remainder * base);
binaryVal = binaryVal / 10;
base = base * 2;
}
return decimal;
}
}
Please Enter Number = 11011010
Result = 218
Please Enter Number = 1111111
Result = 127