Binary to Decimal
Convert a binary number string to its decimal value.
package DSA.Functions;
import java.util.Scanner;
public class BinarytoDecimal { public static int convert(int n) { int f = 0; int count = 0; while (n > 0) { int x = n % 10; int d = (int) ((x) * Math.pow(2, count)); f = f + d; n = n / 10; count++; } return f;
}
public static void main(String[] args) { try (Scanner sc = new Scanner(System.in)) { System.out.println("Enter the binary number"); int a = sc.nextInt(); int f = convert(a); System.out.println("The Decimal is : " + f); } catch (Exception e) { System.out.println("Error is : " + e); } }
}