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
commit acb40ef3b0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

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