mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-08 02:04:31 +08:00
Merge pull request #791 from Priyansh-Kedia/master
Update BinaryToDecimal.java
This commit is contained in:
@ -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();
|
||||||
|
Reference in New Issue
Block a user