(Printing the Decimmal Equivalent of a Binary Number) Input an integer containing only 0s and 1s (i.e., a “binary” integer) and print its decimal equivalent. Use the modulus and division operators to pick off the “binary” number’s digits one at a time from right to left. Much as in the decimal number system, where the rightmost digit has a positional value of 1, the next digit left has a positional value of 10, then 100, then 1000, and so on, in the binary number system the rightmost digit has a positional value of 1, the next digit left has a positional value of 2, then 4, then 8, and so on. Thus the decimal number 234 can be interpreted as 2 * 100 + 3 * 10 + 4 * 1. The decimal equiv- alent of binary 1101 is 1 * 1 + 0 * 2 + 1 * 4 + 1 * 8 or 1 + 0 + 4 + 8, or 13. [Note: To learn more abo

What will be an ideal response?


```
// Convert a binary value to its decimal equivalent.
#include
using namespace std;

int main()
{
int binary; // binary value
int bit = 1; // bit positional value
int decimal = 0; // decimal value

// prompt for and read in a binary number
cout << "Enter a binary number: ";
cin >> binary;

// convert to decimal equivalent
while ( binary != 0 )
{
decimal += binary % 10 * bit;
binary /= 10;
bit *= 2;
} // end while loop

cout << "Decimal is: " << decimal << endl;
} // end main
```

Computer Science & Information Technology

You might also like to view...

Which of the following allows windows to quickly be resized by clicking the title bar of the window and dragging it to the top, sides, or middle of the screen?

A. Drag B. Task View C. Snap D. Shake

Computer Science & Information Technology

What directs computer activities such as communicating with application software?

A) Aero Peek B) Charms bar C) start screen D) operating system

Computer Science & Information Technology

Which of the following features is unique to macOS?

A. User interface B. Multitasking C. Resource forks D. GUI (graphical user interface)

Computer Science & Information Technology

The Printer Event1         P1 requests and is allocated the printer R1.2         P1 releases the printer R1.3         P2 requests and is allocated the disk drive R2.4         P2 releases the disk R2.5         P3 requests and is allocated the plotter R3.6         P3 releases the plotter R3. Assume that the events in the accompanying narrative take place. Which of the following statements is true?

A. There is no deadlock. B. Event 4 caused deadlock. C. Event 5 caused deadlock. D. Event 6 caused deadlock.

Computer Science & Information Technology