Golang 计算矩形面积
本教程将讨论如何使用两种方法在Golang编程中找到矩形的面积−
- 使用长度和宽度计算矩形的面积
-
使用对角线和宽度计算矩形的面积
矩形
矩形是一个有四条边的二维形状。矩形的对边相等,且矩形的所有角都是直角。矩形的另一个特性是它的对边是平行的。
矩形的面积
矩形边界内部的总空间被称为矩形的面积。
其面积可以通过将矩形的长度和宽度相乘来计算。
使用长度和宽度计算矩形的面积
给定矩形的长度’l’和宽度’b’,我们需要找出矩形的面积。
公式
\mathrm{面积 \, =\, 长度 * 宽度}
示例
- 长度= 5
宽度= 2
面积= 5 * 2 = 10
由于面积是通过将长度和宽度相乘计算的,因此我们将5和2相乘,得到面积为10。
- 长度= 25.5
宽度= 5.0
面积= 127.5
面积是通过将长度和宽度相乘计算的,因此−
面积= 25.5 * 5.0
= 127.5
步骤
步骤1 − 声明两个变量-一个用于存储矩形的长度’l’,另一个用于存储矩形的宽度’b’。
步骤2 − 声明一个变量来存储矩形的面积- ‘area’。
步骤3 − 通过将矩形的长度’l’和宽度’b’相乘来计算面积,并将其存储在’area’变量中。
步骤4 − 打印计算得到的面积即变量’area’中存储的值。
使用整数变量
示例
package main
// fmt package allows us to print formatted strings
import "fmt"
// function to calculate area of the rectangle using int variable
func calculateAreaOfRectangle(l, b int) {
// integer type variable ‘area’ to store area of the rectangle
var area int
// calculating area by multiplying length ‘l’ and breadth ‘b’
// of the rectangle
area = l * b
fmt.Println("Length ‘l’ =",l)
fmt.Println("Breadth ‘b’ =",b)
// printing area of the rectangle
fmt.Println("Therefore, Area of Rectangle : ", area)
}
// driver function
func main() {
// declaring variables for storing length ‘l’ and breadth ‘b’
var l, b = 3, 4
// calling the calculateAreaOfRectangle function for
// calculating the area of the rectangle and passing values
// for length and breadth as parameters
calculateAreaOfRectangle(l, b)
}
输出
Length ‘l’ = 3
Breadth ‘b’ = 4
Therefore, Area of Rectangle : 12
说明
在上面的代码中,使用以下公式计算矩形的面积:
面积 = 长度 * 宽度
因此,面积 = 3 * 4
面积 = 12
使用浮点数变量
示例
package main
// fmt package allows us to print formatted strings
import "fmt"
// function to calculate the area of the rectangle using float variable
func calculateAreaOfRectangle(l, b float32) {
// float type variable ‘area’ to store area of the rectangle
// float32 variable stores smaller value
// float64 variable stores larger value
var area float32
// calculating area by multiplying length ‘l’ and breadth ‘b’
// of the rectangle
area = l * b
fmt.Println("Length ‘l’ =",l)
fmt.Println("Breadth ‘b’ =",b)
// printing area of the rectangle
fmt.Println("Therefore, Area of Rectangle : ", area)
}
// driver function
func main() {
// declaring variables for storing length ‘l’ and breadth ‘b’
var l, b float32 = 5.5, 8.5
// calling the calculateAreaOfRectangle function for
// calculating the area of the rectangle and passing values
// for length and breadth as parameters
calculateAreaOfRectangle(l, b)
}
输出
Length ‘l’ = 5.5
Breadth ‘b’ = 8.5
Therefore, Area of Rectangle : 46.75
解释
区域=5.5 * 8.5
=46.75
通过对角线和宽度找到矩形的面积
矩形有两条对角线,两者大小相等。可以使用对角线和宽度来计算矩形的面积。
公式
\mathrm{面积 = 宽度 \times (\sqrt{((对角线)^{2} – (宽度)^{2}))}}
示例
- 对角线=30.0
宽度=10.0
面积=282.842712474619根据公式计算面积,我们使用对角线和宽度的值-
面积 = 宽度 * (√((对角线)2 - (宽度)2))
因此,面积 = 10 * (√((30)2 - (10)2))
面积 = 282.842712474619
示例
package main
// fmt package allows us to print formatted strings
// math package allows us to perform various mathematical
// operations
import (
"fmt"
"math"
)
// function to calculate area of rectangle using float variables
func calculateAreaOfRectangle(d, b float64) {
// float64 type variable ‘area’ to store area of rectangle
var area float64
// calculating area of the rectangle
// math.Sqrt function calculates square root
// math.Pow function calculates the power
fmt.Println("Diagonal ‘d’ =", d)
fmt.Println("Breadth ‘b’ =", b)
// calculating area
area = b * math.Sqrt((math.Pow(d, 2) - math.Pow(b, 2)))
// printing area of the rectangle
fmt.Println("Therefore, Area of Rectangle : ", area)
}
// driver function
func main() {
// declaring variables for storing diagonal ‘d’ and breadth ‘b’
var d, b float64 = 40.0, 20.0
// calling the calculateAreaOfRectangle function for
// calculating the area of the rectangle and passing values
// for diagonal and breadth as parameters
calculateAreaOfRectangle(d, b)
}
输出
Diagonal ‘d’ = 40
Breadth ‘b’ = 20
Therefore, Area of Rectangle : 692.820323027551
解释
在这个公式中,我们使用对角线和宽度的值来计算面积-
面积=宽度*(√((对角线)2 - (宽度)2))
因此,面积=20*(√((40)2-(20)2))
面积=692.820323027551
结论
这就是使用两种方法-长度和宽度,以及对角线和宽度来计算矩形面积的全部内容。我们还在本文中讨论了使用两种不同的数据类型作为输入,即整数和浮点数
您可以使用这些教程进一步探索关于Golang编程的更多内容。