In this source code example, we will write a Java program to convert binary numbers to decimal equivalent numbers.
Check out Java 8 examples at Java 8 Examples
Checkout all Java programs at 50+ Java Programs
Java Convert Binary to Decimal
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();
}
}
Output
Enter Binary number: 1111
Decimal equivalent: 15
Check out Java 8 examples at Java 8 Examples
Checkout all Java programs at 50+ Java Programs
Comments
Post a Comment