Golang 如何找到长方体的表面积和体积
在本教程中,我们将看到用Golang程序来求一个长方体的表面积和体积。面积是指任何封闭图形所覆盖的总空间。体积是指容器内容纳东西的能力。
公式
l – 长方体的长度
h–长方体的高度
w–长方体的宽度。
长方体的表面积 – **2*l*w + 2*w*h + 2*l*h**
例如,长方体的长度是10厘米,高度是5厘米,宽度是8厘米,所以长方体的表面积是-
l = 10厘米
h = 5 cm
w=4厘米
面积=2lw+2wh+2lh
= 2104 + 245 + 2510
= 80 + 40 + 100
= 220 cm^2
长方体的体积 – **l * b * h**
例如,长方体的长度为10厘米,高度为5厘米,宽度为8厘米,那么长方体的体积为-
l = 10厘米
h = 5 cm
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步 – 以长方体的长、宽、高和the为参数调用函数,并存储函数返回的体积。
第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中查找长方体的面积和体积的两种方法。第二种方法在模块化和代码重用性方面要好得多,因为我们可以在项目的任何地方调用这个函数。