The user is first prompted to enter a radius. Then the circumference and area of the circle are calculated, rounded and output to the screen.
Line | Description |
---|---|
1 | Includes the iostream header for standard input and output |
2 | Includes the math.h header to access the mathematical functions |
5 | This line defines useful mathematical constants that can then be accessed |
8 | Declares the variables r , area and circumference of type float |
10 – 11 | Prompts the user to enter a radius. The value is stored in the variable named r |
14 | Calculates the circumference of the circle with the formula 2 * M_PI * r . The number PI can be used directly because the mathematical constants (_USE_MATH_DEFINES ) have been defined. The constant for the number PI is M_PI . The result of this calculation is stored in the variable circumference |
18 | Calculates the circular area of the circle with the formula M_PI * pow(r, 2) . The pow() function calculates the power of a number by raising the first argument to the second argument. The result of this calculation is stored in the variable area |
20 | Outputs the circumference |
21 | Outputs the circular area |
#include <iostream>
#include <math.h>
using namespace std;
#define _USE_MATH_DEFINES
int main () {
float r, area, circumference;
cout << "Please enter radius: ";
cin >> r;
// calculation of circumference
circumference = 2 * M_PI * r;
// calculation of circular area
// pow(base, exponent): Returns base raised to the power exponent
area = M_PI * pow(r, 2);
cout << "Circumference: " << circumference << endl;
cout << "Circular Area: " << area << endl;
}
Please enter radius: 7
Circumference: 43.9823
Circular Area: 153.938