Golang 如何查找圆的周长

Golang 如何查找圆的周长

在本教程中,我们将看到Golang程序来寻找圆的周长。周长是指任何封闭图形的边界的总长度。

如何用Golang查找圆的周长?

公式

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(“The perimeter of a Circle whose radius is”, radius, “=”, perimeter, “cm.”) – 打印圆的周长。

在单独的函数中寻找圆的周长

算法

第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(“The perimeter of a Circle whose radius is”, radius, “is”, perimeter, “cm.”)- 打印圆的周长。

总结

以上是在Golang中查找圆的周长的两种方法。第二种方法在模块化和代码重用性方面要好得多,因为我们可以在项目的任何地方调用这个函数。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程