尋找給定三條邊的三角形面積的 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