编写程序,输入圆半径r,求圆周长c和面积s,结果保留两位小数。 用5种方式输出同样的结果。
时间: 2023-12-14 11:36:48 浏览: 67
好的,以下是 Python 代码实现:
```python
import math
# 输入半径r
r = float(input("请输入圆的半径:"))
# 计算圆的周长和面积
c = 2 * math.pi * r
s = math.pi * r ** 2
# 输出结果方式1
print("圆的周长为:{:.2f}".format(c))
print("圆的面积为:{:.2f}".format(s))
# 输出结果方式2
print("圆的周长为:" + format(c, ".2f"))
print("圆的面积为:" + format(s, ".2f"))
# 输出结果方式3
print(f"圆的周长为:{c:.2f}")
print(f"圆的面积为:{s:.2f}")
# 输出结果方式4
print("圆的周长为:%.2f" % c)
print("圆的面积为:%.2f" % s)
# 输出结果方式5
print("圆的周长为:", round(c, 2))
print("圆的面积为:", round(s, 2))
```
以上代码中,math.pi表示圆周率,format函数可以用来格式化输出数据,%.2f表示保留两位小数,f字符串可以在字符串中插入变量,round函数可以将数字四舍五入到指定位数。