計算已知對角線的菱形面積和周長的程式
C++ 中的菱形是什麼?


什麼是菱形?

在幾何學中,菱形是具有四條等長邊的四邊形。菱形的形狀類似於鑽石。如果菱形的對角線成直角相交,則它成為正方形。

菱形的特性包括:

  • 等邊
  • 對邊平行且對角相等,使其成為平行四邊形
  • 對角線互相垂直平分

以下是菱形的圖形

問題

給定對角線,例如 d1 和 d2,任務是找到菱形的面積和周長,其中面積是形狀佔據的空間,周長是其邊界將覆蓋的空間。

計算長方體的面積和周長,可以使用以下公式:

示例

Input-: d1=6 and d2=12
Output-: The perimeter of rhombus with given diagonals are :26
   The area of rhombus with given diagonals are :36

演算法

Start
Step 1 -> declare function to calculate perimeter of rhombus
   int perimeter(int d1, int d2)
      Declare variable long long int perimeter
      Set perimeter = 2 * sqrt(pow(d1, 2) + pow(d2, 2))
      Print perimeter
Step 2 -> Declare function to calculate area of rhombus
   int area(int d1, int d2)
      Declare long long int area
      Set area = (d1 * d2) / 2
      Print area
Step 3 -> In main()
   Declare variable int d1 = 6, d2 = 12
   Call perimeter(d1, d2)
   Call area(d1, d2)
Stop

示例

#include <iostream>
#include <math.h>
using namespace std;
// program to calculate perimeter of rhombus
int perimeter(int d1, int d2){
   long long int perimeter;
   perimeter = 2 * sqrt(pow(d1, 2) + pow(d2, 2));
   cout<< "The perimeter of rhombus with given diagonals are :"<<perimeter;
}
//program to calculate area of rhombus
int area(int d1, int d2){
   long long int area;
   area = (d1 * d2) / 2;
   cout<<"
The area of rhombus with given diagonals are :"<< area; } int main(){    int d1 = 6, d2 = 12;    perimeter(d1, d2);    area(d1, d2);    return 0; }

輸出

The perimeter of rhombus with given diagonals are :26
The area of rhombus with given diagonals are :36

更新於:2019年9月20日

瀏覽量:207

開啟你的職業生涯

完成課程獲得認證

開始學習
廣告