Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C# Program to Calculate the Area of a Circle
A circle is a round two-dimensional shape where all points on the boundary are equidistant from the center. The distance from the center to any point on the circle's edge is called the radius. Calculating the area of a circle is a fundamental geometric operation commonly used in programming.
Formula for Circle Area
The area of a circle is calculated using the formula
Area = ? × r²
Where:
-
? (pi) ? 3.14159 or
Math.PIin C# - r is the radius of the circle
Using Direct Formula Approach
The simplest approach calculates the area directly using Math.PI and Math.Pow() method
using System;
class Program {
static void Main() {
double radius = 5;
// Calculate area using direct formula
double area = Math.PI * Math.Pow(radius, 2);
// Display the result
Console.WriteLine($"Radius: {radius}");
Console.WriteLine($"Area: {area:F2} square units");
}
}
The output of the above code is
Radius: 5 Area: 78.54 square units
Using Function-Based Approach
Creating a separate function makes the code reusable and more organized
using System;
class CircleCalculator {
static double CalculateCircleArea(double radius) {
if (radius < 0) {
throw new ArgumentException("Radius cannot be negative");
}
return Math.PI * Math.Pow(radius, 2);
}
static void Main() {
double[] radii = {3, 7.5, 10};
Console.WriteLine("Circle Area Calculator");
Console.WriteLine("=====================");
foreach (double radius in radii) {
double area = CalculateCircleArea(radius);
Console.WriteLine($"Radius: {radius} ? Area: {area:F2}");
}
}
}
The output of the above code is
Circle Area Calculator ===================== Radius: 3 ? Area: 28.27 Radius: 7.5 ? Area: 176.71 Radius: 10 ? Area: 314.16
Using Class-Based Approach
An object-oriented approach encapsulates circle properties and methods
using System;
class Circle {
private double radius;
public Circle(double radius) {
if (radius < 0) {
throw new ArgumentException("Radius must be non-negative");
}
this.radius = radius;
}
public double GetArea() {
return Math.PI * radius * radius;
}
public double GetCircumference() {
return 2 * Math.PI * radius;
}
public void DisplayInfo() {
Console.WriteLine($"Circle with radius: {radius}");
Console.WriteLine($"Area: {GetArea():F2}");
Console.WriteLine($"Circumference: {GetCircumference():F2}");
}
}
class Program {
static void Main() {
Circle circle1 = new Circle(4);
Circle circle2 = new Circle(6.5);
circle1.DisplayInfo();
Console.WriteLine();
circle2.DisplayInfo();
}
}
The output of the above code is
Circle with radius: 4 Area: 50.27 Circumference: 25.13 Circle with radius: 6.5 Area: 132.73 Circumference: 40.84
Comparison of Approaches
| Approach | Best For | Advantages |
|---|---|---|
| Direct Formula | Simple one-time calculations | Quick and straightforward |
| Function-Based | Multiple calculations, reusability | Organized, reusable, error handling |
| Class-Based | Complex applications, OOP design | Encapsulation, multiple properties |
Conclusion
Calculating the area of a circle in C# can be accomplished using direct formulas, functions, or object-oriented approaches. The Math.PI constant and Math.Pow() method provide accurate calculations, while different approaches offer varying levels of code organization and reusability.
