在 C++ 中求三角形的周長


在這個問題中,我們將瞭解三角形的周長、不同型別三角形的周長公式以及計算它們的程式。

周長定義為圖形周圍的總距離。基本上,它是給定圖形所有邊的總和。

三角形的周長

三角形的周長是其三條邊之和(三角形是一個三邊形)。

公式:

Perimeter = sum of all sides

Perimeter = x + y + z

計算三角形周長的程式:

示例

 線上演示

#include <iostream>
using namespace std;
int calcPerimeter(int x, int y, int z ){
   int perimeter = x + y + z;
   return perimeter;
}
int main(){
   int x = 5, y = 7, z = 8;
   cout<<"The side of the triangle are \n";
   cout<<"X = "<<x<<"\tY = "<<y<<"\tZ = "<<z<<endl;
   cout<<"The perimeter of the triangle is "<<calcPerimeter(x, y, z);
   return 0;
}

輸出

三角形的邊長為

X = 5 Y = 7 Z = 8
The perimeter of the triangle is 20

不同型別三角形的周長:

在數學中,有不同型別的三角形,它們具有一些特殊的性質。雖然周長的基本公式保持不變,但所有型別的三角形都有特定的公式。讓我們看看每一個。

等邊三角形

這是一種特殊的三角形,其中所有邊和角都相等。

Perimeter = 3*a

計算等邊三角形面積的程式:

示例

 線上演示

#include <iostream>
using namespace std;
int calcPerimeter(int a){
   int perimeter = 3*a;
   return perimeter;
}
int main(){
   int a = 5;
   cout<<"The side of the equilateral triangle are \n";
   cout<<"a = "<<a<<endl;
   cout<<"The perimeter of the triangle is "<<calcPerimeter(a);
   return 0;
}

輸出

等邊三角形的邊長為

a = 5
The perimeter of the triangle is 15

等腰三角形

這是一種特殊的三角形,其中兩條邊相等,第三條邊的長度不同。

Perimeter = 2*X + Y

計算等腰三角形周長的程式:

示例

 線上演示

#include <iostream>
using namespace std;
int calcPerimeter(int x, int y){
   int perimeter = 2*x + y;
   return perimeter;
}
int main(){
   int x = 5, y = 8;
   cout<<"The side of the Isosceles triangle are \n";
   cout<<"X = "<<x<<"\tY = "<<y<<endl;
   cout<<"The perimeter of the triangle is "<<calcPerimeter(x, y);
   return 0;
}

輸出

等腰三角形的邊長為

X = 5 Y = 8
The perimeter of the triangle is 18

不等邊三角形

這是一個三條邊都不相同的三角形。

周長 = x + y + z

更新於:2021年3月16日

2K+ 瀏覽量

開啟您的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.