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

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

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

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

圆锥体

圆锥体是一个三维图形,它的顶部是尖的,底部是平的和弯曲的。圆锥体的高度用 “h “表示,平坦的弧形表面的半径用 “r “表示。冰淇淋筒就是一个很好的圆锥体的例子。

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

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

圆锥体的体积

一个圆锥体所占据的空间的容量或数量被称为它的体积。在我们想知道一个甜筒能装多少冰淇淋的情况下,计算一个甜筒的体积是有好处的。

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

圆锥体的面积

圆锥体所占的总面积被称为圆锥体的表面积。

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

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

例子

  • 高度=7, 半径=5

圆锥体的体积=183.333333333331

圆锥体的面积=213.75082562495555

解释

  • 圆锥体的体积=(⅓)π(r)2 *h

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

= 183.33333333333331

  • 对于圆锥体的面积,首先计算’l’的值

长度’l’ = √(r2 + h2 ))

= √(52 + 72 ))

= 8.602

现在把’l’的值放入圆锥体的面积公式中–

圆锥体的面积=π*r * (r + l)

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

= 213.75082562495555

计算圆锥体的体积和面积

算法

第1步 --声明一个变量用于存储圆锥体的高度–“h”。

第2步 --声明一个用于存储圆锥体半径的变量–‘r’。

第3步 --声明两个变量用于存储圆锥体的面积–“面积 “和圆锥体的体积–“体积”,并将这两个变量的初始值设为0。

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

第5步 --用√(r2 + h2 )计算’l’的值,以便在计算圆锥体面积的公式中使用。

第6步 --用公式计算面积–面积=πr(r+l),并将其存入函数calculateAreaOfCone()中的’面积’变量。

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

例子

package main

// fmt package allows us to print formatted strings
import (
   "fmt"
   "math"
)
func calculateVolumeOfCone(h, r float64) float64 {

   // declaring variable ‘volume’ for storing the volume of the cone
   var volume float64 = 0

   // calculating the volume of the cone using its formula
   volume = (1.0 / 3.0) * (22.0 / 7.0) * r * r * h
   return volume
}
func calculateAreaOfCone(h, r float64) float64 {
   // declaring variable ‘area’ for storing the area of the cone
   var area float64 = 0

   // calculating the value of length 'l'
   l := math.Sqrt((r * r) + (h * h))

   // calculating the area of the cone using its formula
   area = (22.0 / 7.0) * r * (r + l)
   return area
}
func main() {

   // declaring variable ‘height’ for storing the height of the cone
   var h float64 = 7

   // declaring variable ‘radius’ for storing the radius of the cone
   var r float64 = 5

   // declaring variables ‘area’ and ‘volume’ for storing the area and volume of the cone
   var area, volume float64
   fmt.Println("Program to calculate the volume and area of the Cone \n")

   // calling function calculateVolumeOfCone() for
   // calculating the volume of the cone
   volume = calculateVolumeOfCone(h, r)

   // calling function calculateAreaOfCone() for calculating
   // the area of the cone
   area = calculateAreaOfCone(h, r)

   // printing the results
   fmt.Println("Height of the cone : ", h)
   fmt.Println("Radius of the cone : ", r)
   fmt.Println("Therefore, Volume of cone : ", volume)
   fmt.Println("Area of cone : ", area)
}

输出

Program to calculate the volume and area of the Cone 

Height of the cone :  7
Radius of the cone :  5
Therefore, Volume of cone :  183.33333333333331
Area of cone :  213.75082562495555

代码的描述

  • var h float64 = 7, var r float64 = 5 – 在这一行,我们声明了高度 “h “和半径 “r “的变量。

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

  • volume = (1.0 / 3.0) * (22.0 / 7.0) * r * r * h – 如果我们希望输出是数据类型为float,我们明确需要转换这些值,这就是我们使用22.0和7.0而不是22和7的原因。使用上述公式,我们可以计算出圆锥体的体积。

  • return volume – 用于返回圆锥体的体积。

  • volume = calculateVolumeOfCone(h, r) – 我们正在调用函数calculateVolumeOfCone(),并将计算值存储在主方法中的’volume’变量中。

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

  • l := math.Sqrt((r * r) + (h * h)) – 我们将需要长度’l’的值来计算圆锥体的面积。

  • area = (22.0 / 7.0) * r * (r + l) – 使用这个公式,我们计算出圆锥的面积。

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

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

结论

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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程