C# program to find Area of Circle
Program
using System;
namespace AreaOfCircle
{
class Program
{
static void Main(string[] args)
{
float pie = 3.14f;
float radius, area;
Console.Write("Enter the radius of the circle:\t");
radius = float.Parse(Console.ReadLine());
area = pie * radius * radius;
Console.WriteLine("Area of Circle is:\t" + area);
}
}
}
This C# program calculates the area of a circle based on the user-provided radius.
1. Namespace and Class Definition
- The program is inside the
AreaOfCircle
namespace. - The
Program
class contains theMain()
method where the logic is written.
2. Declaring Variables
pie
is set to3.14f
(approximate value of π).- Two
float
variables,radius
andarea
, are declared to store the user input and the computed area.
3. Taking User Input
- The program prompts the user to enter the radius.
- The input is taken as a string and converted to a
float
usingfloat.Parse()
.
4. Calculating the Area
- The formula for the area of a circle is applied:
[
\text{Area} = \pi \times \text{radius}^2 ] - The calculated area is stored in the
area
variable.
5. Printing the Result
- The area of the circle is displayed using
Console.WriteLine()
.
Key Points:
- Uses float data type to handle decimal values accurately.
- Formula used: Area = π × radius × radius.
- Prompts user for input and dynamically calculates the area.
- Can be improved by using
Math.PI
instead of a hardcoded π value for better precision.
Output
Enter the radius of the circle: 20
Area of Circle is: 1256