Write program to read three numbers and display each output
Solution
6. Setw is used to set field width for characters (i.e escape that many character with space).
#include <iostream>
#include <iomanip>
using namespace std;
int main () {
long double a,b,c;
cin>>a>>b>>c;
cout<< a<<endl;
cout << setw(10);
cout << b << endl;
cout << setw(50);
cout << c << endl;
return 0;
}
In second line of output it can be seen that,number starts after 15 character width.
2. It is used to set the decimal precision to format float values on output operations.
// setprecision example
#include <iostream> // std::cout, std::fixed
#include <iomanip> // std::setprecision
using namespace std;
int main () {
double a,b,c;
cin>>a>>b>>c;
cout << a << \'\ \';
cout << setprecision(3) << b << \'\ \';
cout << setprecision(5) << c << \'\ \';
return 0;
}
In output , 2nd and 3rd line you can see there are change in precision after decimal point wrt to the argument n function.
12.
// setprecision example
#include <iostream> // std::cout, std::fixed
#include <iomanip> // std::setprecision
using namespace std;
int main () {
float hours,rate;
cin>>hours>>rate;
cout << hours<< setw(10) << rate << \'\ \';
return 0;
}
hours & rate is seprted by 10 character widths.

