Calculate the Area of a Circle

Calculate the Area of a Circle in C++

Master the use of constants and mathematical formulas by building a program that computes the area of a circle using a radius provided by the user.

Logic and Constants

The const Keyword

Using const double PI ensures that the value of π cannot be modified during program execution, preventing errors in geometric calculations.

Area Formula

We apply the formula $A = \pi \cdot r^2$ by multiplying the radius by itself: PI * radius * radius.

Step-by-Step Objective

  • Define a const double for PI (3.14159).
  • Request the circle's radius from the user.
  • Execute the calculation: area = PI * radius * radius.
  • Output the result with a descriptive message.

C++ Solution

circle_area.cpp C++ Beginners
#include <iostream>
using namespace std;

int main() {
    const double PI = 3.14159;
    double radius, area;

    cout << "Enter the radius of the circle: ";
    cin >> radius;

    // Calculation logic
    area = PI * radius * radius;

    cout << "The area of the circle is: " << area << endl;

    return 0;
}

Console Output:

Enter the radius of the circle: 3
The area of the circle is: 28.2743
Constants & Geometric Logic
Constant Variables (const) Geometric Formulas Double Precision Math Squaring Numbers Data Protection Academic C++ Examples

Share this C++ Exercise

Help others learn temperature conversion in C++.

© C++ Programming Exercises. Master the art of performance coding.