Circumference and circular area

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.

Code Explanation

LineDescription
1Includes the iostream header for standard input and output
2Includes the math.h header to access the mathematical functions
5This line defines useful mathematical constants that can then be accessed
8Declares the variables r, area and circumference of type float
10 – 11Prompts the user to enter a radius. The value is stored in the variable named r
14Calculates 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
18Calculates 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
20Outputs the circumference
21Outputs 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;
}
Output
Please enter radius: 7
Circumference: 43.9823
Circular Area: 153.938