Write a C++ program that makes use of the cout statements and prints the following values to the screen.

Write a C++ program that makes use of the cout statements and prints the following values to the screen.
4 // printing an integer
_ _ 4 // printing within 3 spaces
4 _ _ // printing within 3 spaces but left adjusted
3.141559 // printing a floating-point value
3.14 // printing floating-point value using 2 digits after the decimal point
_ _ 3.14 // printing floating-point value within 6 spaces, 2 digits after the decimal point

Solution :
Input Screen

#include
#include
using namespace std;
int main()
{
int a = 2;
char b = ‘f’;
float c = 3.1415f;
double d = 3;
// printing within 3 spaces
cout << setw(3) << a << endl; // printing within 3 spaces but left adjusted cout << setw(3) << left << a << endl; // printing within 3 spaces but right adjusted cout << setw(3) << right << a << endl; //printing with tab cout << '\t' << a << '\t' << b << '\t' << c << endl; //printing within 9 spaces a, within 8 spaces b and 13 spaces for c cout << setw(9) << a << setw(8) << b << setw(13) << c << endl; cout << d << '\t' << setprecision(1) << d << '\t' << fixed << setprecision(1) << d << endl; cout.unsetf(ios::fixed); cout << d << endl; return 0; }

OutPut Screen

Leave a Comment

Your email address will not be published. Required fields are marked *