Golang程序 计算圆柱体的体积和面积

Golang程序 计算圆柱体的体积和面积

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

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

圆柱体

圆柱体是一个三维图形,它的底面是圆形的。两个底面之间的距离被称为圆柱体的高度’h’,底面的半径用’r’表示。一个冷饮罐就是一个很好的圆柱体的例子。

计算圆柱体的体积和面积的Golang程序

圆柱体的体积

圆柱体的容量通常被称为其体积。在我们想知道瓶子或容器的容量的情况下,计算圆柱体的体积是有好处的。

计算圆柱体的体积和面积的Golang程序

圆柱体的面积

圆柱体所包围的总面积被称为圆柱体的表面积。

计算圆柱体的体积和面积的Golang程序

例子

  • 高度=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步 --声明一个用于存储圆柱体面积的变量–“面积”,声明另一个用于存储体积的变量–“体积”,并将这两个变量的初始值定为0。

第4步 --用公式计算体积–体积=π*(r)2 *h,并将其存储在函数calculateVolumeOfCylinder()中的’体积’变量中。

第5步 --用公式计算面积–面积=2πr*(h+r),并将其存入函数calculateAreaOfCylinder()中的’面积’变量中。

第6步 - 打印计算出的圆柱体的体积和面积,即存储在’体积’和’面积’变量中的值。

例子

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’的变量。由于高度和半径的数据类型是float,这就是为什么我们要使用float64数据类型。

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

  • volume = (22.0 / 7.0) * r * r * h – 如果我们希望输出是数据类型为float,我们明确需要转换数值,这就是我们使用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)– 因为我们希望输出是数据类型为float,所以我们在这里也明确需要转换数值,这就是我们使用22.0和7.0而不是22和7的原因。使用上述公式,我们计算出圆柱体的面积。

  • return area – 用于返回圆柱体的面积

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

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

= 550

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

= 377.1428571428571

结论

这就是关于使用Go编程计算圆柱体的体积和面积的全部内容。我们还通过使用单独的函数计算面积和体积来保持代码的模块化,这也增加了代码的可重用性。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程