如何在 Go 語言中求圓的周長?
在本教程中,我們將學習 Go 語言程式來求圓的周長。周長是任何閉合圖形邊界線的總長度。

公式
Perimeter of Circle - 2 * 22 / 7 * r r - radius of a Circle
例如,一個圓的半徑是 10 釐米,那麼圓的周長是 -
perimeter = 2 * 22 / 7 * 10 = 62.8571428
在函式中求圓的周長
演算法
步驟 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 radius of the circle also a variable perimeter to store perimeter var radius, perimeter float64 fmt.Println("Program to find the perimeter of a Circle.") // initializing the radius of a circle radius = 5 // finding the perimeter of a Circle using the formula // and stored in the perimeter variable of float64 type perimeter = 2 * (22 / 7.0) * radius // printing the result fmt.Println("The perimeter of a Circle whose radius is", radius, "=", perimeter, "cm.") fmt.Println("(Finding the perimeter of a circle within the function)") }
輸出
Program to find the perimeter of a Circle. The perimeter of a Circle whose radius = 5 is 31.428571428571427 cm. (Finding the perimeter of a circle within the function)
程式碼描述
var radius, perimeter float64 - 在這一行中,我們聲明瞭稍後將要使用的半徑和周長。由於半徑或周長可能是小數,因此我們使用了 float 資料型別。
perimeter = 2 * (22 / 7.0) * radius - 在這行程式碼中,我們應用了公式並求出了周長。
fmt.Println("半徑為", radius, "的圓的周長為", perimeter, "釐米。")
- 列印圓的周長。
在單獨的函式中求圓的周長
演算法
步驟 1 - 宣告 float64 資料型別的半徑和周長變數。
步驟 2 - 使用相應的值初始化半徑變數。
步驟 3 - 使用半徑作為引數呼叫函式,並存儲函式返回的周長。
步驟 4 - 列印結果。
示例 2
在這個例子中,我們將透過定義一個單獨的函式來求圓的周長,從而求出圓的周長。
package main // fmt package provides the function to print anything import ( "fmt" ) // function has a perimeter of float64 type and has a return type of loat64 func perimeterOfCircle(radius float64) float64 { // returning the perimeter by applying the formula return 2 * (22 / 7.0) * radius } func main() { // declaring the floating variables using the var keyword for // storing the radius of the circle also a variable perimeter to store perimeter var radius, perimeter float64 fmt.Println("Program to find the perimeter of a Circle.") // initializing the radius of a circle radius = 5 // finding the perimeter of a Circle using a separate function perimeter = perimeterOfCircle(radius) // printing the result fmt.Println("The perimeter of a Circle whose radius is", radius, "is", perimeter, "cm.") fmt.Println("(Finding the perimeter of a circle in the separate function)") }
輸出
Program to find the perimeter of a Circle. The perimeter of a Circle whose radius = 5 is 31.428571428571427 cm. (Finding the perimeter of a circle in the separate function)
程式碼描述
var radius, perimeter float64 - 在這一行中,我們聲明瞭稍後將要使用的半徑和周長。由於半徑或周長可能是小數,因此我們使用了 float 資料型別。
radius = 5 - 初始化圓的半徑。
perimeter = perimeterOfCircle(radius) - 在這行程式碼中,我們呼叫了計算圓周長的函式。
fmt.Println("半徑為", radius, "的圓的周長為", perimeter, "釐米。")
- 列印圓的周長。
結論
以上是兩種在 Go 語言中求圓周長的方法。從模組化和程式碼可重用性的角度來看,第二種方法更好,因為我們可以在專案的任何地方呼叫該函式。要了解更多關於 Go 的資訊,您可以瀏覽這些 教程。
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP