Golang程序 计算球体的体积和面积
在本教程中,我们将讨论用Golang编程中的半径计算球体的体积和面积的方法。
但在编写代码之前,让我们简单讨论一下球体及其体积和面积。
球体
球体是一个三维图形,所有的点都与中心等距。它没有边缘,也没有面。球体的半径用 “r “来表示。球体是球体的一个很好的例子。
球体的体积
一个球体所占据的空间的容量或数量被称为其体积。计算球体的体积在我们想知道在一个足球里能装多少空气的情况下是有益的。
球体的面积
球体所占的总面积被称为球体的表面积。计算球体的面积在我们想知道给足球表面上漆的费用的情况下是有好处的。
例子
- 半径=5
球体的体积 = 523.8095238095239
球体的面积 = 314.2857142857143
解释
球体体积 = (4/3) * 𝛑 * (r)3
= (4/3) * (22/7) * (5*5)
= 523.8095238095239
球体的面积 = 4 * 𝛑 * (r)2
= 4 * (22/7) * 5 * 5
= 314.2857142857143
- 半径=30
球体的体积=113142.85714285714
球体的面积=11314.285714285714
计算球体的体积和面积
算法
第1步 --声明一个用于存储球体半径的变量–“r”。
第2步 --声明两个变量用于存储球体的面积–“面积 “和球体的体积–“体积”,并将这两个变量的初始值设为0。
第3步 --用公式计算体积–体积=(4/3) * 𝛑 * (r)3 ,并将其存储在函数calculateVolumeOfSphere()中的’体积’变量中。
第4步 --用公式计算面积–Area = 4 * 𝛑 * (r)2 ,并将其存入函数calculateAreaOfSphere()中的’面积’变量中。
第5步 --打印计算出的球体的体积和面积,即存储在’体积’和’面积’变量中的值。
例子
package main
// fmt package allows us to print formatted strings
import "fmt"
func calculateVolumeOfSphere(r float64) float64 {
// declaring variable ‘volume’ for storing the volume of the sphere
var volume float64 = 0
// calculating the volume of the sphere using its formula
volume = (4.0 / 3.0) * (22.0 / 7.0) * r * r * r
return volume
}
func calculateAreaOfSphere(r float64) float64 {
// declaring variable ‘area’ for storing the area of the sphere
var area float64 = 0
// calculating the area of the sphere using its formula
area = 4 * (22.0 / 7.0) * r * r
return area
}
func main() {
// declaring variable ‘radius’ for storing the radius of the sphere
var r float64 = 5
// declaring variables ‘area’ and ‘volume’ for storing the area and volume of the sphere
var area, volume float64
fmt.Println("Program to calculate the volume and area of the Sphere \n")
// calling function calculateVolumeOfSphere() for
// calculating the volume of the sphere
volume = calculateVolumeOfSphere(r)
// calling function calculateAreaOfSphere() for calculating
// the area of the sphere
area = calculateAreaOfSphere(r)
// printing the results
fmt.Println("Radius of the sphere : ", r)
fmt.Println("Therefore, Volume of the sphere : ", volume)
fmt.Println("Area of the sphere : ", area)
}
输出
Program to calculate the volume and area of the Sphere
Radius of the sphere : 5
Therefore, Volume of the sphere : 523.8095238095239
Area of the sphere : 314.2857142857143
代码的描述
- var r float64 = 5 – 在这一行,我们声明了用于存储半径 “r “的变量。由于体积和面积的数据类型是float,这就是我们使用float64数据类型的原因。
-
calculateVolumeOfSphere(r float64) float64 – 这是我们计算球体体积的函数。该函数的参数是数据类型为 float64 的变量’r’,其返回类型也是 float64。
-
volume = (4.0 / 3.0) * (22.0 / 7.0) * r * r * r – 如果我们希望输出是数据类型为float,我们明确需要转换数值,这就是我们使用数值4.0和3.0而不是4和3的原因。使用上述公式,我们可以计算出球体的体积。
-
return volume – 用于返回球体的体积。
-
volume = calculateVolumeOfSphere(r) – 我们正在调用函数calculateVolumeOfSphere()并将计算值存储在main方法中的’volume’变量中。
-
calculateAreaOfSphere(r float64) float64 – 这是我们要计算球体面积的函数。该函数有数据类型为float64的变量’r’作为参数,其返回类型为float64。
-
area = 4 * (22.0 / 7.0) * r * r – 如果我们希望输出为数据类型为float,我们明确需要转换数值,这就是我们使用22.0和7.0而不是22和7的原因。使用这个公式,我们计算出球体的面积。
-
return area – 用于返回球体的面积。
-
area = calculateAreaOfSphere(r) – 我们正在调用函数calculateAreaOfSphere()并将计算值存储在’area’变量中。
-
fmt.Println(“Therefore, Volume of the sphere : “, volume) – 用于打印结果
总结
这就是关于使用Go编程计算球体的体积和面积的全部内容。我们还通过使用单独的函数计算面积和体积来保持代码的模块化,这也增加了代码的可重复使用性。