用 C++ 計算等邊三角形的面積和周長的程式
什麼是等邊三角形?
顧名思義,等邊三角形是指三邊相等的三角形,且其三個內角均為 60°。它也被稱為正三角形,因為它是一個正多邊形。
等邊三角形的性質如下 -
- 三邊等長
- 內角均為 60 度
以下是等邊三角形的圖形
問題
給定等邊三角形的一邊,任務是求出三角形的面積和周長,其中面積是指形狀所佔的空間,周長是指其邊界所佔的空間。
要計算等邊三角形的面積和周長,可以使用以下公式
示例
Input-: side=14.0 Output-: Area of Equilateral Triangle is : 84.8705 Perimeter of Equilateral Triangle: 42
演算法
Start Step 1 -> Declare function to calculate area of equilateral trainagle Float area(float side) Return sqrt(3) / 4 * side * side Step 2 -> Declare function to calculate perimeter of equilateral trainagle Float perimeter(float side) Return 3 * side Step 3 -> In main() float side = 14.0 call area(side) call perimeter(side) Stop
程式碼
#include <bits/stdc++.h> using namespace std; //function to calculate area of equilateral triangle float area(float side){ return sqrt(3) / 4 * side * side; } //function to calculate perimeter of equilateral triangle float perimeter(float side){ return 3 * side; } int main(){ float side = 14.0; cout << "Area of Equilateral Triangle is : "<<area(side); cout << "\nPerimeter of Equilateral Triangle: "<<perimeter(side); return 0; }
輸出
Area of Equilateral Triangle is : 84.8705 Perimeter of Equilateral Triangle: 42
廣告