Python程式計算圓錐的體積和表面積


圓錐是一個三維圖形,它是由從一個公共點到圓形底面所有點的無限條線段連線而成的。這個公共點也稱為頂點。圓錐使用三個維度來測量:圓形底面的半徑、高度和斜高。

圓錐的高度和斜高的區別在於:高度是從頂點到圓形底面中心的測量值,而斜高是從頂點到圓形底面上任意一點的線段長度。

側表面積,也稱為曲面面積,使用斜高和總表面積來測量,也使用斜高加上圓形底面的面積來測量。計算這些面積的公式如下

Lateral Surface Area − πrl
Total Surface Area − πr(r+l)

圓錐的體積定義為圓錐的曲面和圓形底面所包含的空間。

體積− $\mathrm{{\frac{1}{3}\pi r^2h}}$

輸入輸出場景

讓我們來看一些輸入輸出場景 -

假設輸入為圓形底面的半徑、實際高度和斜高,則輸出為 -

Input: (3, 4, 5) // 3 is radius, 4 is actual height and 5 is lateral height
Result: Lateral Surface Area: 47.12388980384689
Total Surface Area: 75.39822368615503
Volume: 37.69911184307752

使用數學公式

我們使用標準數學公式來查詢圓錐的表面積和體積。輸入要求是圓錐的半徑、斜高和實際高度。讓我們看一個簡單的Python示例來更好地理解它。

示例

以下示例實現了計算具有特定半徑和高度的圓錐的表面積和體積。

import math l = 5 h = 4 r = 3 #calculating the lateral surface area lsa = (math.pi)*r*l print("Lateral Surface Area: ", str(lsa)) #calculating the total surface area tsa = (math.pi)*r*(r+l) print("Total Surface Area: ", str(tsa)) #calculating the volume vol = (1/3)*(math.pi)*r*r*h print("Volume: ", str(vol))

輸出

在編譯並執行上面的程式後,輸出結果為 -

Lateral Surface Area: 47.12388980384689
Total Surface Area: 75.39822368615503
Volume: 37.69911184307752

計算面積和體積的函式

Python 還允許使用者定義函式,可以使用def關鍵字宣告,並根據需要提供任意數量的引數。在本例中,我們將建立函式來計算圓錐的表面積和體積。

示例

在下面的示例中,程式接收的輸入將是半徑、高度和斜高。宣告使用者定義函式來計算表面積和體積。

import math def cone_lsa(r, l): #calculating the lateral surface area lsa = (math.pi)*r*l print("Lateral Surface Area: ", str(lsa)) def cone_tsa(r, l): #calculating the total surface area tsa = (math.pi)*r*(r+l) print("Total Surface Area: ", str(tsa)) def cone_vol(r, h): #calculating the volume vol = (1/3)*(math.pi)*r*r*h print("Volume: ", str(vol)) l = 5 h = 4 r = 3 cone_lsa(r, l) cone_tsa(r, l) cone_vol(r, h)

輸出

上面程式碼的輸出顯示如下 -

Lateral Surface Area: 47.12388980384689
Total Surface Area: 75.39822368615503
Volume: 37.69911184307752

更新於: 2022年10月26日

3K+ 瀏覽量

開啟你的職業生涯

透過完成課程獲得認證

開始學習
廣告