如何在 Golang 中求正方形的面積?


在本教程中,我們將瞭解如何使用 Golang 程式來求正方形的面積。面積是指任何封閉圖形所覆蓋的總空間。


公式

Area of Square - (length of a side) * (length of a side)
s - length of a side of a Square

例如,如果正方形的邊長為 10 釐米,那麼正方形的面積為 -

Area = 10 * 10
     = 100 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 side of the Square also a variable area // to store Area var lengthOfSide, Area float64 fmt.Println("Program to find the Area of a Square.") // initializing the length of the side of a Square lengthOfSide = 10 // finding the Area of a Square Area = (lengthOfSide * lengthOfSide) // printing the result fmt.Println("The Area of a Square whose length of the side is", lengthOfSide, "is", Area, "cm * cm.") fmt.Println("(Finding the Area of a Square within the function)") }

輸出

Program to find the Area of a Square.
The Area of a Square whose length of the side is 10 is 100 cm * cm.
(Finding the Area of a Square within the function)

程式碼說明

  • var lengthOfSide, Area float64 - 在這一行中,我們聲明瞭稍後將要使用的邊長和麵積。由於邊長或面積可能為小數,因此我們使用了 float 資料型別。

  • Area = (lengthOfSide * lengthOfSide) - 在這行程式碼中,我們應用了公式並計算了面積。

  • fmt.Println("The Area of a Square whose length of side is", lengthOfSide, "is", Area, "cm * cm.")

    - 列印正方形的面積。

在單獨的函式中求正方形的面積

演算法

步驟 1 - 宣告用於儲存邊長和麵積的 float64 資料型別變數。

步驟 2 - 初始化變數。

步驟 3 - 呼叫該函式並將邊長作為引數傳遞,並將函式返回的面積儲存起來。

步驟 4 - 列印結果。

示例 2

在本示例中,我們將透過定義單獨的函式來求正方形的面積。

package main // fmt package provides the function to print anything import ( "fmt" ) func areaOfSquare(lengthOfSide float64) float64 { // returning the area by applying the formula return (lengthOfSide * lengthOfSide) } func main() { // declaring the floating variables using the var keyword for // storing the length of the side of the Square also a variable area // to store Area var lengthOfSide, Area float64 fmt.Println("Program to find the Area of a Square.") // initializing the length of the side of a Square lengthOfSide = 10 // finding the Area of a Square by calling areaOfSquare() function Area = areaOfSquare(lengthOfSide) // printing the result fmt.Println("The Area of a Square whose length of a side is", lengthOfSide, "is", Area, "cm * cm.") fmt.Println("(Finding the Area of a Square in the separate function)") }

輸出

Program to find the Area of a Square.
The Area of a Square whose length of a side is 10 is 100 cm * cm.
(Finding the Area of a Square in the separate function)

程式碼說明

  • var lengthOfSide, Area float64 - 在這一行中,我們聲明瞭稍後將要使用的邊長和麵積。由於邊長或面積可能為小數,因此我們使用了 float 資料型別。

  • Area = areaOfSquare(lengthOfSide) - 在這行程式碼中,我們呼叫了求正方形面積的函式。

  • fmt.Println("The Area of a Square whose length of side is", lengthOfSide, "is", Area, "cm * cm.") -

    列印正方形的面積。

結論

以上是兩種在 Golang 中求正方形面積的方法。從模組化和程式碼可重用性方面考慮,第二種方法更好,因為我們可以在專案的任何地方呼叫該函式。要了解更多關於 go 的資訊,您可以瀏覽這些 教程

更新於: 2022年9月2日

355 次檢視

開啟你的 職業生涯

完成課程獲得認證

立即開始
廣告

© . All rights reserved.