Golang程序 寻找矩形面积
本教程将讨论如何在Golang编程中使用两种方法来寻找矩形的面积 −
- 用长和宽求长方形的面积
-
使用对角线和宽度的矩形面积
矩形
矩形是一个有四条边的二维形状。矩形的对边是相等的,矩形的所有角度都是90°。长方形的另一个特性是它的对边是相互平行的。
长方形的面积
长方形边界内所包围的总空间被称为长方形的面积。

其面积可以通过乘以长方形的长度和宽度来计算。
用长度和宽度计算长方形的面积
给出一个长方形的长度’l’和宽度’b’,我们需要找出一个长方形的面积。
公式

例子
- 长度= 5
宽度=2
面积= 5 * 2 = 10
由于面积是通过长度和宽度相乘来计算的,因此我们将5和2相乘,得到面积为10。
- 长度= 25.5
宽度= 5.0
面积=127.5
面积的计算方法是将长度和宽度相乘,因此:
面积= 25.5 * 5.0
= 127.5
算法
第1步 --声明两个变量–一个用于存储矩形的长度 “l”,另一个用于存储矩形的宽度 “b”。
第2步 --声明一个变量用于存储矩形的面积–‘面积’。
第3步 -通过乘以矩形的长度’l’和宽度’b’来计算面积,并将其存储在’面积’变量中。
第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
解释
在上面的代码中,面积的计算方法是用矩形的长度和宽度相乘,公式为
面积=L*B
因此,面积= 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
用对角线和宽度求长方形的面积
一个长方形有两条对角线,两条对角线的大小相等。长方形的面积可以用对角线和宽度来计算。

公式

例子
- 对角线= 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
结论
以上是关于用两种方法计算矩形的面积–长和宽,以及对角线和宽。我们还讨论了在本文中使用两种不同的数据类型作为输入,即整数和浮点数。
极客教程