Uses I/O manipulators to display the table of squares and square roots. : cout setw setprecision « Console « C++





Uses I/O manipulators to display the table of squares and square roots.


#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

int main()
{
  double x;

  cout << setprecision(4);
  cout << "      x   sqrt(x)     x^2\n\n";

  for(x = 2.0; x <= 20.0; x++) {
    cout << setw(7) << x << "  ";
    cout << setw(7) << sqrt(x) << "  ";
    cout << setw(7) << x*x << '\n';
  }
 
  return 0;
}



           
       










Related examples in the same category

1.Cout: precision 4Cout: precision 4
2.Demonstrate an I/O manipulator: setprecision(2) and setw(20)Demonstrate an I/O manipulator: setprecision(2) and setw(20)
3.Enters a character and outputs its octal, decimal, and hexadecimal code.