Write a program that prints a table of the binary, octal and hexadecimal equivalents of the decimal numbers in the range 1 through 256. If you are not familiar with these number systems,
Program:
// The oct, hex, and dec identifiers are stream manipulators // oct causes integers to be output in octal, the manipulator // hex causes integers to be output in hexadecimal, and the manipulator // dec causes integers to be output in decimal. #include <iostream> using namespace std; int main() { cout << "Decimal\t\tBinary\t\t\tOctal\tHexadecimal\n"; for ( int loop = 1; loop <= 256; ++loop ) { cout << dec << loop << "\t\t"; // Output binary number int number = loop; cout << ( number == 256 ? '1' : '0' ); int factor = 256; do { cout <<( number < factor && number >= ( factor / 2 ) ? '1' : '0' ); factor /= 2; number %= factor; } while ( factor > 2 ); // Output octal and hexadecimal numbers cout << '\t' << oct << loop << '\t' << hex << loop << endl; } return 0; }
Post A Comment:
0 comments: