Golang程序 计算立方体体积

Golang程序 计算立方体体积

在本教程中,我们将讨论在Golang编程中使用立方体的边来寻找立方体的体积的方法。

但在编写代码之前,让我们简单讨论一下立方体和它的体积。

立方体

一个立方体是一个有六个正方形面的三维图形。立方体的六个面都是正方形的形状。它的长、宽、高是相等的。骰子是立方体的一个常见例子。

立方体的体积

立方体所占据的全部三维空间被称为立方体的体积。在我们想知道一个立方体的容量的情况下,计算立方体的体积是有好处的。

计算立方体体积的Golang程序

计算立方体体积的Golang程序

公式

立方体的体积可以通过将边长乘以三倍来计算。因此,这个公式可以写成

计算立方体体积的Golang程序

例子

  • 边长=6

立方体的体积 = 6 * 6 * 6 = 216

由于体积是由边长乘以三倍来计算的,因此我们将边长-‘6’乘以三倍,即666,得出216的立方体的体积。

  • 边长=12.5

立方体的体积 = 12.5 * 12.5 * 12.5 = 1953.125

将边长-‘12.5’乘以三倍,即12.512.512.5,结果是1953.125为立方体的体积。

在函数中寻找立方体的体积

算法

第1步 --声明一个变量用于存储立方体的边–“边”。

第2步 --声明一个用于存储立方体体积的变量–“体积”,并将其初始化为0。

第3步–通过将边长乘以3倍来计算体积,并将其存储在主函数的’体积’变量中。

第4步 - 打印计算出的体积,即存储在变量’volume’中的值。

例子

package main

// fmt package allows us to print formatted strings
import "fmt"

func main() {
   fmt.Println("Program to find the volume of a cube \n")

   // declaring variable ‘side’ for storing length of cube
   var side float64 = 6

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

   // calculating the volume of the cube
   volume = side * side * side

   // printing the results
   fmt.Println("Dimension of the side : ", side)
   fmt.Println("Therefore, Volume of cube : ", volume)
}

输出

Program to find the volume of a cube 

Dimension of the side :  6
Therefore, Volume of cube :  216

代码的描述

  • var side float64 = 5, var volume float64 = 0 – 在这一行,我们声明了变量side和volume。由于side和volume的数据类型是float,这就是为什么我们使用float64数据类型。

  • volume = side * side * side – 我们使用公式找到立方体的体积- volume = side * side * side。

  • fmt.Println(“Dimension of the side : ” , side) – 打印立方体的侧面。

  • fmt.Println(“Therefore, Volume of cube : “, volume) – 打印计算出的立方体的体积。

因此,体积 = 6 * 6 * 6

体积 = 216

用不同的函数求立方体的体积

算法

第1步 --声明一个变量用于存储立方体的边–“边”。

第2步 --声明一个用于存储立方体体积的变量–“体积”,并将其初始化为0。

第3步–通过将边长乘以3倍来计算体积,并在函数calculateVolumeOfCube()中将其储存在’volume’变量中。

第4步 --通过在main()函数中调用calculateVolumeOfCube()函数来打印计算出的体积,即存储在变量 “volume “中的值。

例子

package main

// fmt package allows us to print formatted strings
import "fmt"
func calculateVolumeOfCube(side float64) float64 {
   // declaring variable ‘volume’ for storing the volume of cube
   var volume float64 = 0

   // calculating the volume of the cube
   volume = side * side * side
   return volume

}
func main() {
   // declaring variable ‘side’ for storing length of cube
   var side float64 = 15
   var volume float64
   fmt.Println("Program to find the volume of a cube \n")

   // calling function calculateVolumeOfCube() for calculating
   // the volume of the cube
   volume = calculateVolumeOfCube(side)

   // printing the results
   fmt.Println("Dimension of the side : ", side)
   fmt.Println("Therefore, Volume of cube : ", volume)
}

输出

Program to find the volume of a cube 

Dimension of the side :  15
Therefore, Volume of cube :  3375

代码的描述

  • var side float64 = 5, var volume float64 – 在这一行,我们声明了变量side和volume。由于side和volume的数据类型是float,这就是为什么我们要使用float64数据类型。

  • calculateVolumeOfCube(side float64) float64 – 这是我们计算立方体体积的函数。该函数的参数是数据类型为float64的变量’side’,其返回类型也是float64。

  • volume = side * side * side – 我们使用这个公式来计算立方体的体积–side * side * side。

  • return volume – 用于返回计算出的立方体的体积。

  • volume = calculateVolumeOfCube(side) – 我们正在调用函数calculateVolumeOfCube(),并将计算值存储在’volume’变量中。

  • fmt.Println(“Dimension of the side : ” , side) – 打印立方体的侧面。

  • fmt.Println(“Therefore, Volume of cube : “, volume) – 打印立方体的计算体积。

因此,体积=151515

体积=3375

结论

这就是关于用两种方法计算立方体的体积的全部内容。第二种方法在代码的可重用性和模块化方面要好得多,因为我们可以从任何地方通过传递不同的值来调用该函数,次数不限。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程