如何在 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.