如何在Go語言中求平行四邊形的面積?
在本教程中,我們將學習Go語言程式如何計算平行四邊形的面積。面積是任何封閉圖形所覆蓋的總空間。

公式
Area of the Parallelogram - base * height b - length of the base of a Parallelogram h - length of the height of a Parallelogram
例如,平行四邊形的底邊長為6釐米,高為4釐米,則平行四邊形的面積為:
b = 6 cm h = 4 cm Area = base * height = 6 * 4 = 24 cm^2
在函式中計算平行四邊形的面積
演算法
步驟1 − 宣告底邊長、高和麵積的變數,資料型別為float64。
步驟2 − 初始化變數。
步驟3 − 使用上述公式在函式中計算面積。
步驟4 − 列印結果。
Time Complexity: O(1) Space Complexity: O(1)
示例1
在這個例子中,我們將學習如何在函式中計算平行四邊形的面積。
package main // fmt package provides the function to print anything import ( "fmt" ) func main() { // declaring the floating variables using the var keyword for // storing the length of the base of the Parallelogram, // length of the height of the Parallelogram and also // a variable area to store Area var base, height, Area float64 fmt.Println("Program to find the Area of a Parallelogram.") // initializing the length of the base of a Parallelogram base = 6 // initializing the length of the height of a Parallelogram height = 4 // finding the Area of a Parallelogram Area = height * base // printing the result fmt.Println("The Area of a Parallelogram with base =", base,"and height =", height, "is", Area, "cm * cm.") fmt.Println("(Finding the Area of a Parallelogram within the function)") }
輸出
Program to find the Area of a Parallelogram. The Area of a Parallelogram with base = 6 and height = 4 is 24 cm * cm. (Finding the Area of a Parallelogram within the function)
在單獨的函式中計算平行四邊形的面積
演算法
步驟1 − 宣告底邊長、高和麵積的變數,資料型別為float64。
步驟2 − 初始化變數。
步驟3 − 呼叫函式,傳入平行四邊形的底邊長和高,並將函式返回的面積儲存起來。
步驟4 − 列印結果。
示例2
在這個例子中,我們將透過定義一個單獨的函式來計算平行四邊形的面積。
package main // fmt package provides the function to print anything import ( "fmt" ) // in this line we have declared the function that have float64 // type parameter and float64 type returntype func areaOfParallelogram(base, height float64) float64 { // returning the area by applying the formula return base * height } func main() { // declaring the floating variables using the var keyword for // storing the length of the base of the Parallelogram, // length of the height of the Parallelogram and also // a variable area to store Area var base, height, Area float64 fmt.Println("Program to find the Area of a Parallelogram.") // initializing the length of the base of a Parallelogram base = 6 // initializing the length of the height of a Parallelogram height = 4 // finding the Area of a Parallelogram by calling the // function with the respective parameters Area = areaOfParallelogram(base, height) // printing the result fmt.Println("The Area of a Parallelogram with base =", base,"and height =", height, "is", Area, "cm * cm.") fmt.Println("(Finding the Area of a Parallelogram in the separate function)") }
輸出
Program to find the Area of a Parallelogram. The Area of a Parallelogram with base = 6 and height = 4 is 24 cm * cm. (Finding the Area of a Parallelogram in the separate function)
結論
以上是兩種在Go語言中計算平行四邊形面積的方法。第二種方法在模組化和程式碼可重用性方面更好,因為我們可以在專案的任何地方呼叫該函式。要了解更多關於Go語言的知識,您可以瀏覽這些教程。
廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP