Example:
(Book
Page: 105)

// This program calculates the area of a circle.
// The formula for the area of a circle is PI times
// The radius squared. PI is 3.14159.
#include <iostream>
#include <math.h> // needed for pow function
(See Book Page 127 for more information)
using namespace std;
int main()
{
const double PI = 3.14159;
double area, radius;
cout << "This program calculates the area of a
circle.\n";
cout << "What is the radius of the circle? ";
cin >> radius;
area = PI * pow(radius, 2.0);
cout << "The area is " << area << endl;
return 0;
}
Output:
This program calculates the area of a circle.
What is the radius of the circle? 2
The area is 12.56
FORMATTING OUTPUT:
The same data can be printed or displayed in several different ways. For example, all of
the following numbers have the same value, although they look different:
720
720.0
720.00000000
7.2e+2
+720.0
The way a value is printed is called its formatting. The
cout
object has a standard way of
formatting variables of each data type. Sometimes, however, you need more control over
the way data is displayed. For formatting manipulator functions we use
<iomanip>
header file in C++ program. There are different type of manipulator functions:
1)
setw
2)
setprecision
3)
fixed
4)
showpoint
5)
left and right
6)
setfill

Reference: Book Page 120
Example:
#include <iostream>
#include <iomanip>
using namespace std;
void main( )
{
double no=100.56;
cout<<setw(10)<<setfill('*')<<"R"<<endl;
cout<<fixed<<setprecision(8)<<no <<endl;
system("pause");
}
Output:
*********R
100.56000000
Lab Tasks:
Task 1
With the help of manipulator functions write the program that produces the following
output
< Solution and output of Task 1 >
Task 2

#### You've reached the end of your free preview.

Want to read all 75 pages?

- Fall '19
- Bilal Shahid
- Control flow, do-while loop