setw() : Setting field width Using Cout in C++ Programming

setw() : Setting field width Using Cout in C++ Programming

  1. setw() is library function in C++.
  2. setw() is declared inside #include<iomanip>
  3. setw() will set field width.
  4. setw() sets the number of characters to be used as the field width for the next insertion operation.

Syntax :

setw(num)
  • num is width to be set in order to insert next value.

Live Example 1 : Setting Width in C++

#include <iostream>#include <iomanip>usingnamespacestd;intmain(){cout<< setw (10);cout<<"Pritesh"<< endl;return0;}
  1. Now Length of String Pritesh is 7
  2. We have set field width 10 so it will utilizes 7 fields
  3. Remaining 3 fields are kept blank.
  4. Default Justification : Left

Live Example 2 : Setting Width in C++

#include <iostream>#include <iomanip>usingnamespacestd;intmain(){constint maxCount =4;constint width =6;int row;int column;for(row=1;row<=10;row++){for(column =1; column <= maxCount; column++){cout<< setw(width)<< row * column;}cout<< endl;}return0;}

Output :

12342468369124812165101520612182471421288162432918273610203040

Lượt xem (675)

Xem Thêm Bài Viết  Hàm setw() trong C++

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *