Golang 如何找到平行四边形的面积
在本教程中,我们将看到在Golang中找到平行四边形的面积的程序。面积是由任何闭合图形覆盖的总空间。
公式
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)
在独立函数中寻找平行四边形的面积
步骤
第一步 - 声明基的长度、高度的长度和浮点型面积的变量。
第二步 - 初始化变量。
第三步 - 调用函数,传入平行四边形的基的长度和高度的长度,并存储函数返回的面积。
第四步 - 打印结果。
示例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)
结论
这是在Golang中找到平行四边形面积的两种方法。就模块化和代码可重用性而言,第二种方法要好得多,因为我们可以在项目中的任何地方调用该函数。要了解更多关于go的知识,您可以探索这些教程。