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
Scripting languages are unique computer languages with a specialized function. explain what scripting languages do, and how this
yarga [219]

Scripting languages are unique computer languages because they automate task that could be done by human operator and are easy to use.

<h3>What is a scripting language?</h3>

Scripting languages are programming languages that is interpreted.

They are programming languages that automates the task that will originally be performed by a human operator.

Scripting languages are used to give instruction to other software to run accordingly to how you want it.

The scripting language is different form other language base on the fact that its interpreted . This means the code are translated to machine code when it is run.

The major advantage of scripting languages is that it is human readable and understandable.

Examples of scripting languages are Python and JavaScript.

learn more on scripting here: brainly.com/question/12763341

#SPJ1

3 0
2 years ago
When you open a program, the hard drive
r-ruslan [8.4K]

the answer is c)loads into the RAM for high speed access.

5 0
3 years ago
Read 2 more answers
Discuss FOUR challenges that have an impact on domestic tourism
shutvik [7]
Crime rate
unemployment
fluctuations
suspension of terrorism
5 0
4 years ago
A form of artificial intelligence that can perform many complex, _______ tasks. Select one: a. serialized b. repetitive c. non-r
Tanya [424]

Answer:

The correct answer is letter "C": non-repetitive.

Explanation:

Artificial Intelligence (AI) refers to all efforts mankind has made to program computer systems in a way they can interact as humans and imitate their actions. There are different types of AI based on their likeness to humankind. In such a scenario, AI can be reactive (respond to stimuli), self-aware (awareness capabilities), limited memory (learns to improve) or theory of mind (understands other intelligencies).

<em />

<em>All those types of AI are based on non-repetitive tasks that can perform different complex processes.</em>

6 0
4 years ago
Write a program that first gets a list of integers from input. The input begins with an integer indicating the number of integer
Xelga [282]

Answer:

import java.util.Scanner;

public class ListOfIntegers {

public static void main(String[] args) {

 // Create an object of the Scanner class to allow for user input

 Scanner input = new Scanner(System.in);

 // Prompt the user for the number of integers they want to enter

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

 // Get and store the number of integers entered by user

 int n = input.nextInt();

 System.out.println();

 // Create an array to hold the integers

 // The array has the same length as the number of integers

 int[] numbers = new int[n];

 // Create a loop that asks the user to enter all integers

 // At each cycle of the loop, save the entered integer into the array

 for (int i = 0; i < n; i++) {

  System.out.print("Enter number " + (i + 1) + " : ");

  numbers[i] = input.nextInt();

  System.out.println();

 }

 // Prompt the user for the lower bound

 System.out.print("Enter the lower bound : ");

 // Get and store the lower bound in a variable

 int lowerbound = input.nextInt();

 System.out.println();

 // Prompt the user for the upper bound

 System.out.print("Enter the upper bound : ");

 // Get and store the upper bound in a variable

 int upperbound = input.nextInt();

 System.out.println();

 System.out.println("OUTPUT :: ");

 // Create a loop to cycle through the array of numbers.

 // At each cycle, check if the array element is within range.

 // If it is within range, print it to the console

 for (int j = 0; j < numbers.length; j++) {

  if (numbers[j] >= lowerbound && numbers[j] <= upperbound) {

   System.out.print(numbers[j] + " ");

  }

 }

}

}

Explanation:

Please go through the comments in the code for more readability and understanding. The source code file has been attached to this response. Kindly download the file to get a better formatting of the code.

Hope this helps!

Download java
7 0
4 years ago
Read 2 more answers
Other questions:
  • Why are video texts an example of multimedia? A. They use audio and visual elements together. B. They use one type of medium to
    13·2 answers
  • Hope wants to add a third use at the end of her
    14·1 answer
  • On what level has social media connected us to one another?
    11·1 answer
  • In which of these places might you be most likely to find a peer-to-peer network? On the Internet In a hospital In a home In a l
    5·2 answers
  • A _____ is inserted so that a portion of a document that can have different formatting from the rest of the document. a. heading
    9·1 answer
  • In the Word 2016 window, where is the Status bar located?
    11·1 answer
  • Budget at completion<br><br> What does this measurement tell you?
    8·1 answer
  • Can someone solve this for me please? It’s part of an escape room.
    13·2 answers
  • Networks and the interconnectivity of media have affected our society in that:
    13·2 answers
  • How can blockchain be used to support sustainable business practices?
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!