寻找给定三条边的三角形面积的 Golang 程序
步骤
- 读取三角形的三条边并将其存储在三个单独的变量中。
- 利用海伦公式计算三角形的面积。
- 打印三角形的面积。
| 输入第一条边:15 输入第二条边:9 输入第三条边:7 三角形的面积为:20.69 | 输入第一条边:5 输入第二条边:6 输入第三条边:7 三角形的面积为:14.7 |
解释
- 用户必须输入所有三个数字并将它们存储在单独的变量中。
- 首先,找出 s 的值为 (a+b+c)/2
- 然后,应用海伦公式以确定由所有三条边形成的三角形的面积。
- 最后,打印三角形的面积。
示例
package main
import (
"fmt"
"math"
)
func main(){
var a, b, c float64
fmt.Print("Enter first side of the triangle: ")
fmt.Scanf("%f", &a)
fmt.Print("Enter second side of the triangle: ")
fmt.Scanf("%f", &b)
fmt.Print("Enter third side of the triangle: ")
fmt.Scanf("%f", &c)
s:=(a+b+c)/2
area:=math.Sqrt(s * (s - a) * (s - b) * (s - c))
fmt.Printf("Area of the triangle is: %.2f", area)
}结果
Enter first side of the triangle: 15 Enter second side of the triangle: 9 Enter third side of the triangle: 7 Area of the triangle is: 20.69
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP