C# Program to Find Area of Circle with Output
To find the area of a circle in C#, use the formula
area = Math.PI * radius * radius where radius is the circle's radius; for example, double area = Math.PI * radius * radius; calculates the area.Examples
Inputradius = 0
OutputArea of circle: 0
Inputradius = 5
OutputArea of circle: 78.53981633974483
Inputradius = 10.5
OutputArea of circle: 346.3605900582744
How to Think About It
To find the area of a circle, first understand that the area depends on the radius squared multiplied by the constant pi. So, you take the radius, multiply it by itself, then multiply by pi. This gives the total space inside the circle.
Algorithm
1
Get the radius value from the user or input.2
Calculate the area by multiplying pi with the square of the radius.3
Display the calculated area.Code
csharp
using System; class Program { static void Main() { Console.Write("Enter radius of circle: "); double radius = Convert.ToDouble(Console.ReadLine()); double area = Math.PI * radius * radius; Console.WriteLine("Area of circle: " + area); } }
Output
Enter radius of circle: 5
Area of circle: 78.53981633974483
Dry Run
Let's trace the example where radius = 5 through the code
1
Input radius
User enters 5, so radius = 5
2
Calculate area
area = Math.PI * 5 * 5 = 3.141592653589793 * 25 = 78.53981633974483
3
Output area
Prints 'Area of circle: 78.53981633974483'
| Step | Radius | Area Calculation | Area Result |
|---|---|---|---|
| 1 | 5 | N/A | N/A |
| 2 | 5 | 3.141592653589793 * 5 * 5 | 78.53981633974483 |
| 3 | 5 | N/A | 78.53981633974483 |
Why This Works
Step 1: Use radius input
The program asks for the radius, which is the distance from the center to the edge of the circle.
Step 2: Calculate area using formula
It uses Math.PI for pi and multiplies it by the radius squared to get the area.
Step 3: Display the result
Finally, it prints the area so the user can see the space inside the circle.
Alternative Approaches
Using a method to calculate area
csharp
using System; class Program { static double CalculateArea(double r) { return Math.PI * r * r; } static void Main() { Console.Write("Enter radius: "); double radius = Convert.ToDouble(Console.ReadLine()); double area = CalculateArea(radius); Console.WriteLine("Area of circle: " + area); } }
This approach separates calculation logic into a method for better code reuse and clarity.
Using inline expression with var
csharp
using System; class Program { static void Main() { Console.Write("Radius: "); var r = double.Parse(Console.ReadLine()); var area = Math.PI * r * r; Console.WriteLine($"Area: {area}"); } }
Using <code>var</code> and string interpolation makes the code shorter and easier to read.
Complexity: O(1) time, O(1) space
Time Complexity
The calculation involves a fixed number of operations (multiplications), so it runs in constant time.
Space Complexity
Only a few variables are used, so the space needed is constant.
Which Approach is Fastest?
All approaches run in constant time and space; differences are mainly in code organization and readability.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Direct calculation in Main | O(1) | O(1) | Simple quick programs |
| Separate method for calculation | O(1) | O(1) | Reusable and clear code |
| Using var and interpolation | O(1) | O(1) | Concise and modern syntax |
Always use
Math.PI for the most accurate value of pi in C#.Beginners often forget to square the radius and just multiply by pi once.