(Number Systems Table) Write a program that prints a table of the binary, octal and hexa- decimal equivalents of the decimal numbers in the range 1–256. If you are not familiar with these number systems, read Appendix D, Number Systems, first. [Hint: You can use the stream manipu- lators dec, oct and hex to display integers in decimal, octal and hexadecimal formats, respectively.]

What will be an ideal response?


```
// Display decimal, binary, octal and hexadecimal numbers.
#include
using namespace std;

int main()
{
int number; // loop counter for binary numbers
int factor; // current factor for binary numbers

// use tabs to display table headers
cout << "Decimal\t\tBinary\t\tOctal\tHexadecimal\n";

// loop from 1 to 256
for ( int loop = 1; loop <= 256; loop++ )
{
cout << dec << loop << "\t\t";

// output binary number
// initialize variables for calculating binary equivalent
number = loop;
factor = 256;

// output first digit
cout << ( number == 256 ? '1' : '0' );

// loop until factor is 1, i.e., last digit
do
{
// output current digit
cout <<
( number < factor && number >= ( factor / 2 ) ? '1' : '0' );
// update factor and number variables
factor /= 2;
number %= factor;
} while ( factor > 1 );

// output octal number using oct stream manipulator
cout << '\t' << oct << loop;

// output hexadecimal number using hex stream manipulator
cout << '\t' << hex << loop << endl;
} // end for
} // end main
```

Computer Science & Information Technology

You might also like to view...

Find the error(s) in the following code. This code should modify the age column of table people.

``` 1 myResultSet = myStatement.executeQuery( 2 "UPDATE people SET age = 30 WHERE name = 'Bob'" ); ```

Computer Science & Information Technology

Suppose console is a Scanner object initialized with the standard input device. The expression console.nextInt(); is used to read one int value and the expression console.nextDouble(); is used to read two int values.

Answer the following statement true (T) or false (F)

Computer Science & Information Technology

Start on Click sets the animation to start when the slide is clicked

Indicate whether the statement is true or false

Computer Science & Information Technology

Even when the worksheet is unprotected, the locked property determines whether or not changes can be made to a given cell.

Answer the following statement true (T) or false (F)

Computer Science & Information Technology