Write the following code in c #,3. Write a method call CalculateAreaOfCircle(). This method will prompt the user for the radius of a circle and then calculate and display the area.[A = πr2]. Call the CalculateAreaOfCircle() method twice from the same Main() method as in question 1. Use Math.Pi for the value of π
时间: 2024-01-31 14:02:13 浏览: 104
Sure, here's the code in C#:
```csharp
using System;
class Program
{
static void Main()
{
// Call the CalculateAreaOfCircle method twice
CalculateAreaOfCircle();
CalculateAreaOfCircle();
}
static void CalculateAreaOfCircle()
{
Console.Write("Enter the radius of a circle: ");
double radius = double.Parse(Console.ReadLine());
double area = Math.PI * radius * radius;
Console.WriteLine($"The area of the circle is: {area}");
}
}
```
This code defines a `Program` class with two methods: `Main` and `CalculateAreaOfCircle`. In the `Main` method, we call `CalculateAreaOfCircle` twice.
The `CalculateAreaOfCircle` method prompts the user for the radius of a circle, reads in the input using `Console.ReadLine`, calculates the area using the formula `A = πr^2`, and then displays the result using `Console.WriteLine`.
The `Math.PI` constant is used to represent the value of `π` in the calculation.
阅读全文