Q:

write a JAVA program that asks the user to enter 4-bit binary number and convert it to decimal

0

write a JAVA program that asks the user to enter 4-bit binary number. 

Note that the user will enter the number bit by bit.

Your program should then calculate and display the equivalent decimal number.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

import java.util.Scanner;

public class Main
{
  public static void main (String[]args)
  {
      Scanner input = new Scanner (System.in);
      String binaryNumber = "";
      
      System.out.println ("enter first bit:");
      binaryNumber =binaryNumber + input.next();
      System.out.println ("enter second bit:");
      binaryNumber =binaryNumber + input.next();
      System.out.println ("enter third bit:");
      binaryNumber =binaryNumber + input.next();
      System.out.println ("enter forth bit:");
      binaryNumber =binaryNumber + input.next();

  // convert to decimal
  int decimal = Integer.parseInt (binaryNumber, 2); //parseInt() converts any number to a decimal number
  System.out.println (binaryNumber + " in binary = " + decimal + " in decimal.");
}
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now