Write a program to calculate the circumference of the circle in C#

using System;

class Program
{
    static void Main()
    {
        double radius;
        Console.WriteLine("Enter the radius of the circle: ");
        if (double.TryParse(Console.ReadLine(), out radius))
        {
            double circumference = 2 * Math.PI * radius;
            Console.WriteLine($"The circumference of the circle is: {circumference:F2}");
        }
        else
        {
            Console.WriteLine("Invalid input. Please enter a valid numeric value for the radius.");
        }
    }
}

In this program:

  1. The user is prompted to enter the radius of the circle.
  2. The double.TryParse method is used to validate the user’s input and convert it to a double value.
  3. If the input is valid, the circumference is calculated using the formula 2 * π * radius, where π is approximated by Math.PI.
  4. The calculated circumference is displayed with two decimal places using the F2 format specifier.
  5. If the input is invalid (not a valid numeric value), an error message is displayed.

When you run the program, you can enter the radius of the circle, and it will calculate and display the circumference based on the entered value.

error: Content is protected !!