编写程序,输入圆的半径r,计算并输出圆的周长和面积
时间: 2024-06-14 19:03:56 浏览: 16
以下是一个编写程序,输入圆的半径r,计算并输出圆的周长和面积的例子:
```python
import math
class MyMath:
def __init__(self, radius):
self.radius = radius
def calculate_circumference(self):
return round(2 * math.pi * self.radius, 2)
def calculate_area(self):
return round(math.pi * self.radius ** 2, 2)
radius = float(input("请输入半径:"))
circle = MyMath(radius)
circumference = circle.calculate_circumference()
area = circle.calculate_area()
print("圆的周长:", circumference)
print("圆的面积:", area)
```
相关问题
编写程序,输入圆的半径r,计算并输出圆的周长和面积.python
以下是使用Python编写的计算圆的周长和面积的程序:
```python
import math
# 输入圆的半径
r = float(input("请输入圆的半径:"))
# 计算周长
circumference = 2 * math.pi * r
# 计算面积
area = math.pi * r * r
# 输出结果
print("圆的周长为:", circumference)
print("圆的面积为:", area)
```
C语言:输入圆的半径r,输出圆的周长和面积
下面是使用C语言编写的计算圆的周长和面积的程序:
```c
#include <stdio.h>
#define PI 3.14159
int main() {
float r, c, s;
printf("请输入圆的半径:");
scanf("%f", &r);
c = 2 * PI * r;
s = PI * r * r;
printf("圆的周长为:%.2f\n", c);
printf("圆的面积为:%.2f\n", s);
return 0;
}
```
程序中使用了宏定义`#define PI 3.14159`来定义圆周率,然后通过用户输入半径r的值,计算出圆的周长和面积。最后使用`printf()`函数输出结果。注意输出时使用`%.2f`来控制输出精度为小数点后两位。