Unit-6: Darshan Institute of Engineering & Technology For Diploma Studies
Unit-6: Darshan Institute of Engineering & Technology For Diploma Studies
Unit-6: Darshan Institute of Engineering & Technology For Diploma Studies
return 0;
}
for(int i=1;i<=10;i++)
{
cout.setf(ios::internal, ios::adjustfield);
cout.width(5);
cout<<i;
cout.setf(ios::right, ios::adjustfield);
cout.width(20);
cout<<sqrt(i)<<"\n";
}
cout.setf(ios::scientific, ios::floatfield);
cout<<"\nSQRT(100)="<<sqrt(100)<<"\n";
return 0;
}
/* OUTPUT
value*******SQRT OF VALUE
+...1.............+1.0000
+...2.............+1.4142
+...3.............+1.7321
+...4.............+2.0000
+...5.............+2.2361
+...6.............+2.4495
+...7.............+2.6458
+...8.............+2.8284
+...9.............+3.0000
+..10.............+3.1623
SQRT(100)=+1.0000e+01 */
• The manipulators are shown in below table:
Manipulators Use
setw() To specify the required field size for displaying an output value.
setprecision() To specify the number of digits to be displayed after the decimal
point of a float value.
setfill() To specify a character that is used to fill the unused portion of a
field.
setiosflags() To specify format flags that can control the form of output
display such as left or right justification.
resetiosflags() To clear the flags specified.
• Manipulators are used to manipulate the output in specific format.
• Example for manipulators
#include <iostream.h>
#include <iomanip.h>
int main()
{
cout.setf(ios::showpoint);
cout<<setw(5)<<"n"
<<setw(15)<<"Inverse of n"
<<setw(15)<<"Sum of terms\n\n";
double term,sum=0;
for(int n=1;n<=10;n++)
{
term=1.0/float(n);
sum=sum+term;
cout<<setw(5)<<n
<<setw(14)<<setprecision(4)
<<setiosflags(ios::scientific)<<term
<<setw(13)<<resetiosflags(ios::scientific)
<<sum<<endl;
}
return 0;
}
/* Output
n Inverse of n Sum of terms
1 1.0000e+00 1.0000
2 5.0000e-01 1.5000
3 3.3333e-01 1.8333
4 2.5000e-01 2.0833
5 2.0000e-01 2.2833
6 1.6667e-01 2.4500
7 1.4286e-01 2.5929
8 1.2500e-01 2.7179
9 1.1111e-01 2.8290
10 1.0000e-01 2.9290 */