//package
import java.util.*;
// the main class BinaryToDecimalCalculator
public class BinaryToDecimalCalculator
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);//scanner object for input
//declaration of input variables
int binary, exponent=0;
float decimal, total=0 , currentNumber ;
boolean error =false;//check for input errors
//the user inputs a binary integer
System.out.print("Enter binary number: ");
binary = input.nextInt();
//processing formular
do
{
//fetches binary digits
decimal = (float)(binary)/((float)10);
binary = binary / 10;
currentNumber = (int)(10 * (decimal - binary));
//prompts the user input when it is not a binary integer
/*if(currentNumber >1 || )
{
System.out.println("The number you typed is not a binary value, re-run the program");
error = true;
break;
}
else
{
//computes the decimal value
total += (currentNumber * (Math.pow(2, exponent)));
exponent++;
}
}while(decimal !=0);//checks whether extraction is finished
if(!error)
System.out.println("The decimal equivalent of binary integer is : "+(int)total);
}
}
popsyjohnson
0
Newbie Poster
Recommended Answers
Jump to PostPlease explain your problem. Show the programs output and add comments showing what the problem is.
For testing how to use operators, write a very small program that uses the operator and print out the results. Change the operands and do it again. Continue until you understand what is …
All 3 Replies
popsyjohnson
0
Newbie Poster
NormR1
563
Posting Sage
Team Colleague
Ezzaral
2,714
Posting Sage
Team Colleague
Featured Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.