C Program to Find Area of a Circular Ring
Introduction
A circular ring is a geometric shape formed by two concentric circles, one inside the other. The area of the ring is the region between the outer circle and the inner circle. Finding the area of a circular ring is a practical problem in fields like engineering, design, and geometry.
In this article, we will write a C program to calculate the area of a circular ring. The program will use user inputs for the radii of the inner and outer circles, compute the area using a mathematical formula, and display the result.
Formula
The formula for calculating the area of a circular ring is:
Area = Ï€ × (R² - r²)
R
is the radius of the outer circle.r
is the radius of the inner circle.Ï€
(Pi) is a mathematical constant approximately equal to 3.14159.
To calculate the area, we find the difference between the squares of the radii of the outer and inner circles, then multiply this difference by π
.
Code
Below is the C program to calculate the area of a circular ring:
#include <stdio.h> #define PI 3.14159 int main() { float R, r, area; // Input radii of the outer and inner circles printf("Enter the radius of the outer circle (R): "); scanf("%f", &R); printf("Enter the radius of the inner circle (r): "); scanf("%f", &r); // Ensure the outer radius is greater than the inner radius if (R <= r) { printf("Error: The outer radius must be greater than the inner radius.\n"); return 1; } // Calculate the area of the circular ring area = PI * (R * R - r * r); // Output the result printf("The area of the circular ring is: %.2f\n", area); return 0; }
Output
Here is an example of how the program works when executed:
Enter the radius of the outer circle (R): 7 Enter the radius of the inner circle (r): 4 The area of the circular ring is: 153.94
Explanation
- Inputs: The program prompts the user to enter the radii of the outer and inner circles. These values are stored as floating-point numbers for accuracy.
- Validation: A check ensures that the outer radius is greater than the inner radius. If not, an error message is displayed, and the program terminates.
- Calculation: Using the formula
Area = Ï€ × (R² - r²)
, the program calculates the area of the circular ring. The constantPI
is defined as 3.14159 for precise computation. - Output: The program prints the area of the circular ring to two decimal places.
This approach ensures the program is user-friendly, accurate, and robust, handling invalid inputs gracefully while delivering correct results.