如何在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语言的知识,您可以浏览这些教程。
广告