Source of number_bases.cpp


  1: //number_bases.cpp
  2: //Displays numbers in decimal, octal and hexadecimal form.

  4: #include <iostream>
  5: #include <iomanip>
  6: using namespace std;

  8: int main()
  9: {
 10:     cout << "\nThis program displays numbers in decimal, octal, and "
 11:         "hexadecimal (\"hex\") forms.\nTo make sense of everything, "
 12:         "study both the source code and the output.\n\nIn the first "
 13:         "table below, look at any row.  Each value is written using\n"
 14:         "the same digits (in the source code), but in a different base, "
 15:         "and is\noutput in (default) decimal format in each case.\n"
 16:         "Decimal  Octal  Hex\n"
 17:         "-------------------\n";
 18:     cout << setw(4) <<     6
 19:          << setw(8) <<    06
 20:          << setw(7) <<   0x6 << endl
 21:          << setw(4) <<    14
 22:          << setw(8) <<   014
 23:          << setw(7) <<  0x14 << endl
 24:          << setw(4) <<   123
 25:          << setw(8) <<  0123
 26:          << setw(7) << 0x123 << endl;

 28:     cout << "\nIn this second table, every number actually has the "
 29:         "same value (61 decimal),\nbut each of the three rows shows "
 30:         "the values written using a different base.\n"
 31:         "Dec/Oct/Hex: 61   075   0X3D\n"
 32:         "----------------------------\n";
 33:     cout << "Decimal: "
 34:          << setw(6) <<   61       //Decimal output is the default.
 35:          << setw(6) <<  075
 36:          << setw(6) << 0X3D << endl
 37:          << "Octal:   "
 38:          << setw(6) << oct << 61  //Now we use the "oct" manipulator.
 39:          << setw(6) << oct << 075
 40:          << setw(6) << oct << 0X3D << endl
 41:          << "Hex:     "
 42:          << setw(6) << hex << 61  //And now the "hex" manipulator.
 43:          << setw(6) << hex << 075
 44:          << setw(6) << hex << 0X3D << endl;
 45:     cout << endl;
 46: }