1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Anuta_ua [19.1K]
3 years ago
12

Write a program named BinaryToDecimal.java that reads a 4-bit binary number from the keyboard as a string and then converts it i

nto decimal. For example, if the input is 1100, the output should be 12. (Hint: break the string into substrings and then convert each substring to a value for a single bit. If the bits are b0, b1, b2, b3, then decimal equivalent is 8b0 4b1 2b2 b3)
Computers and Technology
1 answer:
Mars2501 [29]3 years ago
5 0

Answer:

import java.util.*;

public class BinaryToDecimal

{

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    String binaryNumber;

    int decimalNumber = 0;

   

 System.out.print("Enter the binary number: ");

 binaryNumber = input.nextLine();

 

 for(int i=0; i<binaryNumber.length(); i++){

     char c = binaryNumber.charAt(binaryNumber.length()-1-i);

     int digit = Character.getNumericValue(c);

     decimalNumber += (digit * Math.pow(2, i));

 }

 System.out.println(decimalNumber);

}

}

Explanation:

Create a Scanner object to be able to get input from the user

Declare the variables

Ask the user to enter the binaryNumber as a String

Create a for loop that iterates through the binaryNumber. Get each character using charAt() method. Convert that character to an integer value using Character.getNumericValue(). Add the multiplication of integer value and 2 to the power of i to the decimalNumber variable (cumulative sum)

When the loop is done, print the decimalNumber variable

You might be interested in
1.07 (PX: 2 points)Some early computers protected the operating system by placing it in a memory partition that could not be mod
Anna11 [10]

Answer:

The user cannot make internal changes, or personalize the computer. Also it would make it very easy for viruses to get into the computers and ccorrupt the entire thing. There is a similar example of this today in Modern Day chromebooks, but earlier computers were not as safe or secure. Also the fact that it can takes up massive amounts of data, requiring more server storage or data storage facilities.

6 0
3 years ago
You have posted some embarrassing photos of a friend onto the Internet. After
e-lub [12.9K]

Answer:

C

Explanation:

4 0
3 years ago
Research and describe surround sound. What are the different types of surround sound? How is it used? How might surround sound a
Reptile [31]

Surround sound was first application was in movie theaters. Prior to surround sound, theater sound systems commonly had three "screen channels" of sound, from loudspeakers located in front of the audience at the left, center, and right. Surround sound adds one or more channels from loudspeakers behind the listener, able to create the sensation of sound coming from any horizontal direction 360° around the listener. Surround sound formats vary in reproduction and recording methods along with the number and positioning of additional channels.  Multichannel audio techniques may be used to reproduce contents as varied as music, speech, natural or synthetic sounds for cinema, television, broadcasting, or computers.

5 0
3 years ago
Can anyone tell me what's wrong with my pc. When I turn it on it lights up the ram, motherboard, and fans. The cooler and case f
Mice21 [21]

1 or 2 ways I can try to figure out and see what is causing your computer to do this. 1 when was the last time you shut off  OR was doing on the computer? If your problem gets fixed make sure you close all running apps so the apps don't update while your computer is shutdown. 2 did you or something unplug the cables? Or maybe a virus caused something. Try to unplug every cable and wait about 5-10 minutes and if your computer is still not working, I would advise contacting an administrator.

Hope this helped.

6 0
3 years ago
Help?
maxonik [38]

Answer:The answer is A:swift

Explanation:

7 0
3 years ago
Other questions:
  • g Write a program that prompts the user to enter two integers. The program outputs how many numbers are multiples of 3 and how m
    11·1 answer
  • You can choose to use a window manager only, and not use a desktop manager.
    8·2 answers
  • ____ variables are variables that are shared by every instantiation of a class. local instance
    8·1 answer
  • A stop sign is an example of?
    11·2 answers
  • Here is over 40pts as Promised? :)<br> Who invented the Lamborghini?
    12·2 answers
  • What is the output of the code below assuming that global variable x has value 2 and global y has value 3? def f1(): return "ab"
    13·1 answer
  • A network administrator enters the service password-encryption command into the configuration mode of a router. What does this c
    11·1 answer
  • What is Ce stands for?
    8·1 answer
  • Any websites online to make $50 daily?​
    11·1 answer
  • Sequence structure is when have a repetition of instructions. true false
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!