forked from phishman3579/java-algorithms-implementation
-
Notifications
You must be signed in to change notification settings - Fork 7
Java Convert Binary to Decimal
Ramesh Fadatare edited this page Aug 11, 2020
·
1 revision
In this source code example, we will write a Java program to convert binary number to decimal equivalent number.
This class converts a Binary number to a Decimal number:
package net.sourcecodeexamples.java.Conversions;
import java.util.Scanner;
/**
* This class converts a Binary number to a Decimal number
*
*/
class BinaryToDecimal {
/**
* Main Method
*
* @param args Command line arguments
*/
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
int binNum, binCopy, d, s = 0, power = 0;
System.out.print("Enter Binary number: ");
binNum = scanner.nextInt();
binCopy = binNum;
while (binCopy != 0) {
d = binCopy % 10;
s += d * (int) Math.pow(2, power++);
binCopy /= 10;
}
System.out.println("Decimal equivalent:" + s);
scanner.close();
}
}
Enter Binary number: 1111
Decimal equivalent: 15