0
0
CppProgramBeginner · 2 min read

C++ Program to Find Area of Circle with Example

To find the area of a circle in C++, use the formula area = 3.14159 * radius * radius and print the result. For example: double area = 3.14159 * radius * radius;
📋

Examples

Inputradius = 0
OutputArea of circle: 0
Inputradius = 5
OutputArea of circle: 78.53975
Inputradius = 10
OutputArea of circle: 314.159
🧠

How to Think About It

To find the area of a circle, first get the radius from the user. Then multiply the radius by itself and by the constant pi (about 3.14159). This gives the area. Finally, show the result.
📐

Algorithm

1
Get the radius value from the user
2
Calculate area using formula: area = pi * radius * radius
3
Display the calculated area
💻

Code

cpp
#include <iostream>
using namespace std;

int main() {
    double radius, area;
    const double pi = 3.14159;
    cout << "Enter radius: ";
    cin >> radius;
    area = pi * radius * radius;
    cout << "Area of circle: " << area << endl;
    return 0;
}
Output
Enter radius: 5 Area of circle: 78.53975
🔍

Dry Run

Let's trace the example where radius = 5 through the code

1

Input radius

User enters radius = 5

2

Calculate area

area = 3.14159 * 5 * 5 = 78.53975

3

Print area

Output: Area of circle: 78.53975

VariableValue
radius5
area78.53975
💡

Why This Works

Step 1: Get radius input

We ask the user to enter the radius, which is the distance from the center to the edge of the circle.

Step 2: Calculate area

We use the formula area = pi * radius * radius because the area of a circle depends on the square of its radius.

Step 3: Display result

We print the calculated area so the user can see the size of the circle's surface.

🔄

Alternative Approaches

Use M_PI constant from <cmath>
cpp
#include <iostream>
#include <cmath>
using namespace std;

int main() {
    double radius, area;
    cout << "Enter radius: ";
    cin >> radius;
    area = M_PI * radius * radius;
    cout << "Area of circle: " << area << endl;
    return 0;
}
Using <cmath> and M_PI gives a more precise value of pi but requires including the cmath header.
Use float instead of double
cpp
#include <iostream>
using namespace std;

int main() {
    float radius, area;
    const float pi = 3.14159f;
    cout << "Enter radius: ";
    cin >> radius;
    area = pi * radius * radius;
    cout << "Area of circle: " << area << endl;
    return 0;
}
Using float uses less memory but is less precise than double.

Complexity: O(1) time, O(1) space

Time Complexity

The program performs a fixed number of operations regardless of input size, so it runs in constant time O(1).

Space Complexity

Only a few variables are used to store radius and area, so space complexity is O(1).

Which Approach is Fastest?

All approaches run in constant time; using M_PI is slightly more precise but does not affect speed significantly.

ApproachTimeSpaceBest For
Using constant piO(1)O(1)Simple and clear code
Using M_PI from O(1)O(1)More precision with standard constant
Using float typeO(1)O(1)Less memory use, less precision
💡
Always use double for better precision when working with decimal numbers like pi.
⚠️
Beginners often forget to square the radius and just multiply by pi once.