编程求圆面积代码怎么写

时间:2025-01-27 14:52:47 网络游戏

C++

```cpp

include

include

include

using namespace std;

int main() {

double r;

cout << "请输入圆的半径: ";

cin >> r;

double area = M_PI * r * r;

cout << fixed << setprecision(2) << "圆的面积为: " << area << endl;

return 0;

}

```

Python

```python

import math

def calculate_circle_area(radius):

return math.pi * radius 2

radius = float(input("请输入圆的半径: "))

circle_area = calculate_circle_area(radius)

print("圆的面积为:", circle_area)

```

Java

```java

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("请输入圆的半径: ");

double radius = scanner.nextDouble();

double area = Math.PI * radius * radius;

System.out.printf("圆的面积为: %.2f%n", area);

}

}

```

C

```csharp

using System;

class Program {

static void Main() {

Console.Write("请输入圆的半径: ");

double radius = Convert.ToDouble(Console.ReadLine());

double area = Math.PI * radius * radius;

Console.WriteLine("圆的面积为: {0:0.00}", area);

}

}

```

这些代码示例分别使用 C++、Python、Java 和 C 编程语言,通过不同的方式获取用户输入的圆的半径,并使用圆面积公式 πr² 计算面积,最后将结果输出到控制台。