寫一個 python 程序,利用半徑、環境和直徑來找到圓的面積。圓的 Python 面積是圓內的平方單位數。計算圓面積的標準公式是:A=πr。
用半徑求圓面積的 Python 程序
如果我們知道半徑,那麼我們可以用公式計算圓的面積:A=πr(這裡 A 是圓的面積,r 是半徑)。在這個 python 程序中,我們將使用半徑找到圓的面積。
# Python Program to find Area Of Circle using Radius
PI = 3.14
radius = float(input(' Please Enter the radius of a circle: '))
area = PI * radius * radius
circumference = 2 * PI * radius
print(" Area Of a Circle = %.2f" %area)
print(" Circumference Of a Circle = %.2f" %circumference)
我們將 pi 定義為全局變量,賦值為 3.14。這個 python 程序允許用戶輸入半徑值。然後,它會根據公式計算圓的面積。一個圓的 Python 面積的輸出是
Please Enter the radius of a circle: 6
Area Of a Circle = 113.04
Circumference Of a Circle = 37.68
用圓周求圓面積的 Python 程序
繞圓的距離稱為周長。如果你知道周長,那麼我們可以用公式計算圓的面積:A = C4π(這裡 C 是周長)
import math
circumference = float(input(' Please Enter the Circumference of a circle: '))
area = (circumference * circumference)/(4 * math.pi)
print(" Area Of a Circle = %.2f" %area)
Python 面積的一個圓使用圓周輸出
Please Enter the Circumference of a circle: 26
Area Of a Circle = 53.79
首先,我們導入了數學庫,這支持我們使用 Python 編程中的所有數學函數。在這個 Python 的例子中,我們可以使用 math.pi 來調用 PI 值
import math
python 程序的下一行允許用戶輸入周長值。
circumference = float(input(' Please Enter the Circumference of a circle: '))
利用周長,本程序將按照公式計算圓的面積:A = C‖4π
用直徑計算圓面積的 Python 程序
穿過圓心的距離稱為直徑。如果我們知道直徑,那麼我們可以用公式計算圓的面積:A=π/4*D (D 是直徑)
import math
diameter = float(input(' Please Enter the Diameter of a circle: '))
area1 = (math.pi/4) * (diameter * diameter)
# diameter = 2 * radius
# radius = diameter/2
radius = diameter / 2
area2 = math.pi * radius * radius
print(" Area of Circle using direct formula = %.2f" %area1);
print(" Area of Circle Using Method 2 = %.2f" %area2)
圓程序的這個面積允許用戶輸入直徑值。接下來,它將根據我們上面顯示的公式計算圓的面積。
我們還提到了其他方法。
直徑= 2 *半徑
半徑=直徑/2
面積= π 半徑半徑
原創文章,作者:簡單一點,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/129635.html