// This code illustrates formatting with iostreams. // It should display something like this: // // Decimal Octal Hex Squared Root // 1 1 1 1 1.000 // 2 2 2 4 1.414 // 4 4 4 16 2.000 // 8 10 8 64 2.828 // 16 20 10 256 4.000 // 32 40 20 1024 5.657 // 64 100 40 4096 8.000 // 128 200 80 16384 11.314 // 256 400 100 65536 16.000 // 512 1000 200 262144 22.627 // 1024 2000 400 1048576 32.000 #include // cout #include // setfill, setprecision, setw #include // sqrt using namespace std; int main() { constexpr int width=10; // width of table columns cout << fixed << setprecision(3); // show floating-point as x.yyy // cout << showbase; // leading 0/0x for octal/hex // cout << left; // left-justify: value on the left // cout << setfill('*'); // Use this fill character with setw // cout << scientific; // show floating-point as x.yyyezz cout << setw(width) << "Decimal" << setw(width) << "Octal" << setw(width) << "Hex" << setw(width) << "Squared" << setw(width) << "Root" << '\n'; for (int i=1; i<=1024; i*=2) cout << setw(width) << dec << i << setw(width) << oct << i << setw(width) << hex << i << setw(width) << dec << i*i << setw(width) << sqrt(i) << '\n'; }