settingsAccountsettings
By using our mini forum, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy
Menusettings

Q: How to calculate the area of a circle in C#?

+5 votes

I need to write a program that reads the radius r of a circle and prints its area formatted with 2 digits after the decimal point. Examples:

r

area

OUTPUT:

2

12.5663706143592

12.57

3.5

38.484510006475

38.48

 

asked in C# category by user ak47seo
edited by user golearnweb

3 Answers

+2 votes
 
Best answer

Formula for calculating the area of a circle:

area of a circle formula


and the code:

using System;

class CircleArea
{
    static void Main()
    // Area of a circle: A=πr2
    {
        Console.WriteLine("Please write the radius of your circle and hit Enter afterwards: ");
        double radius = double.Parse(Console.ReadLine());
        double pi = Math.PI;
        double area = pi * (radius * radius);
        Console.WriteLine("The Area (A=πr2) of your circle is: {0:0.00}", area);
    }
}

For formatting 2 digits after the decimal point I used: {0:0.00}

but you can also use {0:0.F2}

answered by user eiorgert
edited by user golearnweb
+2 votes

Here's my code:

using System;

class CircleArea
{
    static void Main()
    {
        Console.Write("Radius: ");
        double radius = double.Parse(Console.ReadLine());
        double area = Math.PI * (radius * radius);

        Console.WriteLine("Area = {0:F2}", area);
    }
}

 

answered by user andrew
edited by user golearnweb
+2 votes

...and mine:

using System;

public class Program
{
    public static void Main()
    {
        double r = double.Parse(Console.ReadLine());

        Console.WriteLine("The area of circle is: {0:F2}", Math.PI * r * r);
    }
}
answered by user nikole
edited by user golearnweb
...