This C++ code calculates the circumference and area of a circle based on a given radius.
#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
#define _USE_MATH_DEFINES
cmath or math.h, constants such as M_PI (which represents the value of π) become available. This is particularly useful since M_PI is used to calculate the circumference and area of the circle.float r, area, circumference;
cout << "Please enter radius: ";
cin >> r;
float are declared: r, area, and circumference. The variable r will store the radius of the circle.r using cin.circumference = 2 * M_PI * r;
M_PI provides the value of π, and r is the radius entered by the user. The result is stored in the circumference variable.area = M_PI * pow(r, 2);
pow function is used to square the radius (pow(r, 2)), which is then multiplied by π (M_PI) to find the area. The result is stored in the area variable.cout << "Circumference: " << circumference << endl;
cout << "Circular Area: " << area << endl;
endl is used after each output to insert a newline character and flush the stream, ensuring that all output is immediately displayed.