C++ Program to Find the Area of a Circular Ring
Introduction
A circular ring is a geometric figure that consists of the area between two concentric circles. This shape is commonly encountered in engineering, architecture, and design. rc Calculating the area of a circular ring is a straightforward task that requires the radii of the inner and outer circles.
In this article, we will explore the concept of a circular ring, the mathematical formula for its area, and its implementation in C++ with a detailed explanation and output example.
Formula
The area of a circular ring can be calculated by subtracting the area of the inner circle from the area of the outer circle. The formula is as follows:
- Area of a Circle:
A = Ï€ × r²
, wherer
is the radius of the circle. - Area of a Circular Ring:
A = Ï€ × (R² - r²)
, where:R
is the radius of the outer circle.r
is the radius of the inner circle.
Here, π
(pi) is approximately 3.14159. The difference between the areas of the two circles gives the area of the ring.
Code
Here is the C++ program to compute the area of a circular ring:
#include <iostream> #include <cmath> using namespace std; int main() { // Variables for inner and outer radii double innerRadius, outerRadius; // Input the radii cout << "Enter the radius of the inner circle: "; cin >> innerRadius; cout << "Enter the radius of the outer circle: "; cin >> outerRadius; // Check if outer radius is greater than inner radius if (outerRadius <= innerRadius) { cout << "Error: Outer radius must be greater than inner radius." << endl; return 1; // Exit the program } // Calculate the area of the circular ring double ringArea = M_PI * (pow(outerRadius, 2) - pow(innerRadius, 2)); // Display the result cout << "The area of the circular ring is: " << ringArea << endl; return 0; }
Output
When the program is executed, it prompts the user to enter the radii of the inner and outer circles. Below is an example of the program in action:
Enter the radius of the inner circle: 3 Enter the radius of the outer circle: 5 The area of the circular ring is: 50.2655
Explanation
Let us break down the steps in the program:
-
Input:
The program asks the user to input two radii:
- The radius of the inner circle (
innerRadius
). - The radius of the outer circle (
outerRadius
).
- The radius of the inner circle (
-
Area Calculation:
Using the formula
A = Ï€ × (R² - r²)
, the program calculates the area of the circular ring. Thepow
function from thecmath
library is used to square the radii. -
Output:
The calculated area is displayed to the user in a formatted manner. The value of
Ï€
is provided byM_PI
, a constant defined in thecmath
library.
This program is efficient and demonstrates a practical way of solving problems involving geometric calculations using C++.