Wednesday, November 26, 2014

Use of setwidth operator in c++

Use of setwidth operator in c++




/*use of setw() output formator usually it is automatically right
sewidth which are formatted numbers are written on right side spaces are placed at 
left this is right justified but adding only word left it will become 
left justified we have to only add         <<left     before <<setw()<<value;
numbers are automatically riht justified but if we want to place right word just like
<<right     before stw()<<value    */
 /*in order to make following program right justified just remove     <<left    from syntax */

#include <iostream>
using namespace std;
#include <iomanip>
int main()
{
int n1=3,n2=4,n3=435,n4=2453,n5=4535,
n6=43232,n7=231534,n8=25434,n9=2423;
cout<<left<<setw(7)<<n1<<left<<setw(7)<<n2<<left<<setw(7)<<n3<<endl
<<left<<setw(7)<<n4<<left<<setw(7)<<n5<<left<<setw(7)<<n6<<endl
<<left<<setw(7)<<n7<<left<<setw(7)<<n8<<left<<setw(7)<<n9;
return 0;

}

No comments:

Post a Comment