2019-05-09 19:32:54 +08:00
|
|
|
package Conversions;
|
|
|
|
|
2017-04-18 22:57:17 +08:00
|
|
|
import java.util.Scanner;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This class converts a Binary number to a Decimal number
|
|
|
|
*
|
|
|
|
*/
|
2019-05-09 19:32:54 +08:00
|
|
|
class BinaryToDecimal {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Main Method
|
|
|
|
*
|
|
|
|
* @param args Command line arguments
|
|
|
|
*/
|
|
|
|
public static void main(String args[]) {
|
|
|
|
Scanner sc = new Scanner(System.in);
|
2019-07-10 12:47:08 +08:00
|
|
|
int binNum, binCopy, d, s = 0, power = 0;
|
2019-05-09 19:32:54 +08:00
|
|
|
System.out.print("Binary number: ");
|
2019-07-10 12:47:08 +08:00
|
|
|
binNum = sc.nextInt();
|
|
|
|
binCopy = binNum;
|
|
|
|
while (binCopy != 0) {
|
|
|
|
d = binCopy % 10;
|
2019-07-10 01:36:40 +08:00
|
|
|
s += d * (int) Math.pow(2, power++);
|
2019-07-10 12:47:08 +08:00
|
|
|
binCopy /= 10;
|
2019-05-09 19:32:54 +08:00
|
|
|
}
|
|
|
|
System.out.println("Decimal equivalent:" + s);
|
|
|
|
sc.close();
|
2017-04-18 22:57:17 +08:00
|
|
|
}
|
|
|
|
}
|