Finding area and circumference of a circle
Area and circumference of circle with use of constant value of PI
Learning Objectives
- Declaring constant entities which are read only data types.
- Making formulas with higher precision on left hand side than on the right hand side.
- Use of float, double, long double data type
Source Code
|
|
Run Output
Code Understanding
const float PI = 3.14159;
Here the entity PI has been declared as float value and also modifier const has been used to tell the compiler that this value can not be modified by the programmer later. Means it is a read-only value. By convention in constant entity names are given in all capital letters, the way PI has been given here.
area = PI * radius * radius;
Here the area of circle formula (2 pi r square) has been expressed as c++ expression. This has then be put in variable area. Variable area has been declared as double data type as multiplications of float can give a very large fractional value.
circum=2*PI*radius;
Here the circumference formula (2 pi r) has been expressed and put in variable circum. Variable circum has been given long double data type to demonstrate the handling of very very large fractions with upto 10 bytes storage and 19 display digits precision.
cout << “Area would be : ” << area <<endl;
cout<< “Circumference would be : ” << circum <<endl;
Here area and circumference are being printed.
Notes
- Usually by default only the precision of float which is 7 digits display precision is shown by cout. The double precision of 15 digits and long double precision of 19 digits display is shown only when setprecision function of iomanip definitions are used.
- Precision larger than 19 digit fractions are handled in c++ with the use of special libraries, which beyond the scope of current discussions.
Suggested Filename(s): area-circum.cpp circle-ac.cpp
Created: 20-Dec-2017 03:23 | Updated: 15-Sep-2018 10:51|
