settingsAccountsettings
Menusettings

Q: How to calculate the perimeter (circumference) of a circle in C#?

+4 votes

I need to write a program that reads the radius r of a circle and prints its perimeter (circumference) formatted with 2 digits after the decimal point in C#.

Examples:

r (radius)

perimeter

OUTPUT

2

12.5663706143592

12.57

3.5

21.9911485751286

21.99

 

asked in C# category by user ak47seo

2 Answers

+3 votes
 
Best answer

We can use the following formula for finding the circumference (perimeter) of a circle:

formula for calculating circumference (perimeter) of a circle

We can use the code:

using System;

class CirclePerimeter
{
    static void Main()
    // Perimeter (Circumference) of a circle: C=2πr
    {
        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 = 2 * pi * radius;
        Console.WriteLine("The Perimeter or Circumference (C=2πr) of your circle is: {0:F2}", area);
    }
}
answered by user eiorgert
edited by user golearnweb
+3 votes

this works as well:

using System;

class CirclePerimeter
{
    static void Main()
    {
        Console.Write("Radius: ");
        double radius = double.Parse(Console.ReadLine());

        double perimeter = ((2 * Math.PI) * radius);

        Console.WriteLine("Perimeter = {0:F2}", perimeter);
    }
}

 

answered by user nikole
edited by user golearnweb
...