Golang 如何找到长方体的表面积和体积

Golang 如何找到长方体的表面积和体积

在本教程中,我们将看到在Golang中找到长方体的表面积和体积的程序。面积是任何封闭图形所覆盖的总空间。体积是容器内容纳某物的能力。

Golang 如何找到长方体的表面积和体积

公式

l – 长方体的长度

h – 长方体的高度

w – 长方体的宽度。

长方体的表面积 – 2*l*w + 2*w*h + 2*l*h

例如,长方体的长度为10厘米,高度为5厘米,宽度为8厘米,所以长方体的表面积为−

l = 10厘米

h = 5厘米

w = 4厘米

面积 = 2lw + 2wh + 2lh

= 2104 + 245 + 2510

= 80 + 40 + 100

= 220 cm^2

长方体的体积 – l * b * h

例如,长方体的长度为10厘米,高度为5厘米,宽度为8厘米,所以长方体的体积为-

l = 10厘米

h = 5厘米

w = 4厘米

体积 = l * b * h

= 10 * 5 * 4

= 200

步骤

步骤1 - 声明长度,宽度,高度,体积和面积的变量为float64数据类型。

步骤2 - 初始化变量。

步骤3 - 使用上述公式在函数中找到表面积和体积。

步骤4 - 打印结果。

示例

在这个示例中,我们将在函数中找到长方体的表面积和体积。

package main

// fmt package provides the function to print anything
import (
   "fmt"
)
func main() {

   // declaring the floating variables using the var keyword for

   // storing the length of the parallel sides of the Cuboid,

   // distance between the parallel sides and also a variable area

   // to store Area
   var l, w, h, surfaceArea, volume float64
   fmt.Println("Program to find the Surface Area and volume of a Cuboid.")

   // initializing the length of a Cuboid
   l = 10

   // initializing the width of a Cuboid
   w = 8

   // initializing the height of a Cuboid
   h = 4

   // finding the surface Area of a Cuboid
   surfaceArea = 2*l*w + 2*w*h + 2*l*h

   // finding the volume of a Cuboid
   volume = l * w * h

   // printing the result
   fmt.Println("The Surface Area of a Cuboid whose length , width, and height are", l, ",", w, ",", h, "is", surfaceArea, "cm * cm.")
   fmt.Println("The volume of a Cuboid whose length , width, and height are", l, ",", w, ",", h, "is", volume, "cm * cm * cm.")
   fmt.Println("(Finding the Surface Area and volume of a Cuboid within the function)")
}

输出

Program to find the Surface Area and volume of a Cuboid.
The Surface Area of a Cuboid whose length , width, and height are 10 , 8 , 4 is 304 cm * cm.
The volume of a Cuboid whose length , width, and height are 10 , 8 , 4 is 320 cm * cm * cm.
(Finding the Surface Area and volume of a Cuboid within the function)

步骤

步骤 1 - 声明长度、宽度、高度、体积和浮点64数据类型的面积等变量。

步骤 2 - 初始化变量。

步骤 3 - 以长方体的长度、宽度、高度作为参数调用函数,并存储函数返回的面积。

步骤 4 - 以长方体的长度、宽度、高度作为参数调用函数,并存储函数返回的体积。

步骤 5 - 打印结果。

示例

在这个示例中,我们将通过定义一个单独的函数来计算长方体的面积。

package main

// fmt package provides the function to print anything
import ( 
   "fmt"
)

// in this line we have declared the function that has float64

// type parameter and float64 type returntype
func areaOfCuboid(l, w, h float64) float64 {

   // returning the area by applying the formula
   return 2*l*w + 2*w*h + 2*l*h
}

// in this line we have declared the function that has float64

// type parameter and float64 type returntype
func volumeOfCuboid(l, w, h float64) float64 {

   // returning the volume by applying the formula
   return l * w * h
}
func main() {

   // declaring the floating variables using the var keyword for

   // storing the length of the parallel sides of the Cuboid,

   // distance between the parallel sides and also a variable area

   // to store Area
   var l, w, h, surfaceArea, volume float64
   fmt.Println("Program to find the Surface Area and volume of a Cuboid.")

   // initializing the length of a Cuboid
   l = 10

   // initializing the width of a Cuboid
   w = 8

   // initializing the height of a Cuboid
   h = 4

   // finding the surface Area of a Cuboid by calling the function
   surfaceArea = areaOfCuboid(l, w, h)

   // finding the volume of a Cuboid
   volume = volumeOfCuboid(l, w, h)

   // printing the result 
   fmt.Println("The Surface Area of a Cuboid whose length , width, and height are", l, ",", w, ",", h, "is", surfaceArea, "cm * cm.")
   fmt.Println("The volume of a Cuboid whose length , width, and height are", l, ",", w, ",", h, "is", volume, "cm * cm * cm.")
   fmt.Println("(Finding the Surface Area and volume of a Cuboid in the seperate function)")
}

输出

Program to find the Surface Area and volume of a Cuboid.
The Surface Area of a Cuboid whose length , width, and height are 10 , 8 , 4 is 304 cm * cm.
The volume of a Cuboid whose length , width, and height are 10 , 8 , 4 is 320 cm * cm * cm.
(Finding the Surface Area and volume of a Cuboid in the seperate function)

结论

这是在Golang中找到长方体的面积和体积的两种方法。第二种方法在模块化和代码可重用性方面更好,因为我们可以在项目的任何地方调用该函数。要了解更多关于go的信息,您可以通过这些 教程。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程