Go语言程序:计算圆柱体的体积和表面积


在本教程中,我们将讨论使用Go语言编程,根据圆柱体的高度和半径计算其体积和表面积的方法。

但在编写代码之前,让我们简要讨论一下圆柱体及其体积和表面积。

圆柱体

圆柱体是一个三维图形,其底面为圆形。两个底面之间的距离称为圆柱体的高度“h”,底面的半径用“r”表示。易拉罐就是一个很好的圆柱体例子。


圆柱体的体积

圆柱体的容量通常称为其体积。计算圆柱体的体积在我们需要知道瓶子或容器的容量时非常有用。

=π(r)2h

圆柱体的表面积

圆柱体封闭的总面积称为圆柱体的表面积。

=2πr(h+r)

示例

  • 高度 = 7,半径 = 5

    圆柱体体积 = 550

    圆柱体表面积 = 377.1428571428571

    解释

    圆柱体体积 = π * (r)2 * h

    = (22/7) * 5 * 5 * 7

    = 550

    圆柱体表面积 = 2 * π * r * (h + r)

    = 2 * (22/7) * 5 * (7 + 5)

    = 377.1428571428571

  • 高度 = 21,半径 = 10

    圆柱体体积 = 6600

    圆柱体表面积 = 1948.5714285714284

    解释

    圆柱体体积 = π * (r)2 * h

    = (22/7) * 10 * 10 * 21

    = 6600

    圆柱体表面积 = 2 * π * r * (h + r)

    = 2 * (22/7) * 10 * (21 + 10)

    = 1948.5714285714284

计算圆柱体的体积和表面积

算法

步骤 1 − 声明一个变量用于存储圆柱体的高度 - ‘h’。

步骤 2 − 声明一个变量用于存储圆柱体的半径 - ‘r’。

步骤 3 − 声明一个变量用于存储圆柱体的表面积 - ‘area’,并声明另一个变量用于存储体积 - ‘volume’,并将两个变量都初始化为0。

步骤 4 − 使用公式计算体积 - 体积 = π * (r)2 * h,并将结果存储在函数calculateVolumeOfCylinder()中的‘volume’变量中。

步骤 5 − 使用公式计算表面积 - 表面积 = 2 * π * r * (h + r),并将结果存储在函数calculateAreaOfCylinder()中的‘area’变量中。

步骤 6 − 打印计算出的圆柱体体积和表面积,即存储在‘volume’和‘area’变量中的值。

示例

Open Compiler
package main // fmt package allows us to print formatted strings import "fmt" func calculateVolumeOfCylinder(h, r float64) float64 { // declaring variable ‘volume’ for storing the volume of the cylinder var volume float64 = 0 // calculating the volume of the cylinder using formula volume = (22.0 / 7.0) * r * r * h return volume } func calculateAreaOfCylinder(h, r float64) float64 { // declaring variable ‘area’ for storing the area of the cylinder var area float64 = 0 // calculating the area of the cylinder using formula area = 2 * (22.0 / 7.0) * r * (h + r) return area } func main() { // declaring variable ‘height’ for storing the height of the cylinder var h float64 = 7 // declaring variable ‘radius’ for storing the radius of the cylinder var r float64 = 5 // declaring variable ‘area’ and ‘volume’ for storing the area and volume of the cylinder var area, volume float64 fmt.Println("Program to calculate the volume and area of the Cylinder \n") // calling function calculateVolumeOfCylinder() for // calculating the volume of the cylinder volume = calculateVolumeOfCylinder(h, r) // calling function calculateAreaOfCylinder() for calculating // the area of the cylinder area = calculateAreaOfCylinder(h, r) // printing the results fmt.Println("Height of the cylinder : ", h) fmt.Println("Radius of the cylinder : ", r) fmt.Println("Therefore, Volume of cylinder : ", volume) fmt.Println("Area of cylinder : ", area) }

输出

Program to calculate the volume and area of the Cylinder 

Height of the cylinder :  7
Radius of the cylinder :  5
Therefore, Volume of cylinder :  550
Area of cylinder :  377.1428571428571

代码描述

  • var h float64 = 7, var r float64 = 5 − 在这一行中,我们声明了高度‘h’和半径‘r’的变量。由于高度和半径的数据类型将为浮点数,因此我们使用float64数据类型。

  • calculateVolumeOfCylinder(h, r float64) float64 − 这是我们计算圆柱体体积的函数。该函数的参数是float64数据类型的变量‘h’和‘r’,返回值类型也是float64。

  • volume = (22.0 / 7.0) * r * r * h − 如果我们希望输出为浮点型,我们需要显式地转换值,这就是我们使用22.0和7.0而不是22和7的原因。使用上述公式,我们计算圆柱体的体积。

  • return volume − 返回圆柱体的体积。

  • volume = calculateVolumeOfCylinder(h, r) − 我们调用函数calculateVolumeOfCylinder()并将计算出的值存储在‘volume’变量中。

  • calculateAreaOfCylinder(h, r float64) float64 − 这是我们计算圆柱体表面积的函数。该函数的参数是float64数据类型的变量‘h’和‘r’,返回值类型也是float64。

  • area = 2 * (22.0 / 7.0) * r * (h + r) − 由于我们希望输出为浮点型,因此我们也需要在这里显式地转换值,这就是我们使用22.0和7.0而不是22和7的原因。使用上述公式,我们计算圆柱体的表面积。

  • return area − 返回圆柱体的表面积

  • area = calculateAreaOfCylinder(h, r) − 我们调用函数calculateAreaOfCylinder()并将计算出的值存储在‘area’变量中。

因此,圆柱体的体积 = (22/7) * 5 * 5 * 7

= 550

圆柱体的表面积 = 2 * (22/7) * 5 * (7 + 5)

= 377.1428571428571

Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.

结论

这就是使用Go语言编程计算圆柱体体积和表面积的全部内容。我们还通过使用单独的函数来计算面积和体积来保持代码模块化,这也提高了代码的可重用性。您可以使用这些教程进一步了解Go语言编程。

更新于:2022年11月21日

浏览量 195

开启您的职业生涯

完成课程获得认证

开始学习
广告