Merge pull request #791 from Priyansh-Kedia/master

Update BinaryToDecimal.java
This commit is contained in:
Yang Libin
2019-07-10 13:56:05 +08:00
committed by GitHub

View File

@ -15,14 +15,14 @@ class BinaryToDecimal {
*/ */
public static void main(String args[]) { public static void main(String args[]) {
Scanner sc = new Scanner(System.in); Scanner sc = new Scanner(System.in);
int n, k, d, s = 0, c = 0; int binNum, binCopy, d, s = 0, power = 0;
System.out.print("Binary number: "); System.out.print("Binary number: ");
n = sc.nextInt(); binNum = sc.nextInt();
k = n; binCopy = binNum;
while (k != 0) { while (binCopy != 0) {
d = k % 10; d = binCopy % 10;
s += d * (int) Math.pow(2, c++); s += d * (int) Math.pow(2, power++);
k /= 10; binCopy /= 10;
} }
System.out.println("Decimal equivalent:" + s); System.out.println("Decimal equivalent:" + s);
sc.close(); sc.close();