📜  计算给定对角线的菱形的面积和周长的程序

📅  最后修改于: 2021-05-04 20:36:28             🧑  作者: Mango

给定菱形的对角线长度d1和d2。任务是找到菱形的周长和面积。
菱形是具有四个相等边的多边形,其中相对的两个边都平行,并且相对的角度相等。

菱形

例子:

Input: d1 = 2 and d2 = 4
Output: The area of rhombus with diagonals 2 and 4 is 4.
        The perimeter of rhombus with diagonals 2 and 4 is 8.

Input: d1 = 100 and d2 = 500
Output: The area of rhombus with diagonals 100 and 500 is 25000.
        The perimeter of rhombus with diagonals 100 and 500 is 1019.

下面是上述方法的实现:

C++
// C++ Program to calculate area and perimeter
// of a rhombus using diagonals
#include 
#include 
using namespace std;
 
// calculate area and perimeter of a rhombus
int rhombusAreaPeri(int d1, int d2)
{
    long long int area, perimeter;
 
    area = (d1 * d2) / 2;
    perimeter = 2 * sqrt(pow(d1, 2) + pow(d2, 2));
 
    cout << "The area of rhombus with diagonals "
         << d1 << " and " << d2 << " is " << area << "." << endl;
 
    cout << "The perimeter of rhombus with diagonals "
         << d1 << " and " << d2 << " is " << perimeter << "." << endl;
}
 
// Driver code
int main()
{
 
    int d1 = 2, d2 = 4;
    rhombusAreaPeri(d1, d2);
 
    return 0;
}


Java
// Java program to calculate area and perimeter
// of a rhombus using diagonals
import java.io.*;
 
class GFG {
   
// calculate area and perimeter of a rhombus
static int rhombusAreaPeri(int d1, int d2)
{
     int area, perimeter;
 
    area = (d1 * d2) / 2;
    perimeter = (int)(2 * Math.sqrt(Math.pow(d1, 2) + Math.pow(d2, 2)));
 
    System.out.println( "The area of rhombus with diagonals "
        + d1 + " and " + d2 + " is " + area + ".");
 
    System.out.println("The perimeter of rhombus with diagonals "
        +d1 + " and " + d2 + " is " + perimeter + ".");
        return 0;
}
 
// Driver code
 
 
    public static void main (String[] args) {
        int d1 = 2, d2 = 4;
    rhombusAreaPeri(d1, d2);
    }
}
// This code is contributed by anuj_67..


Python3
# Python 3 Program to calculate
# area and perimeter of a rhombus
# using diagonals
from math import sqrt, pow
 
# calculate area and perimeter
# of a rhombus
def rhombusAreaPeri(d1, d2):
    area = (d1 * d2) / 2
    perimeter = 2 * sqrt(pow(d1, 2) +
                         pow(d2, 2))
 
    print("The area of rhombus with diagonals",
                d1, "and", d2, "is", area, ".")
 
    print("The perimeter of rhombus with diagonals",
                d1, "and", d2, "is", perimeter, "." )
 
# Driver code
if __name__ == '__main__':
    d1 = 2
    d2 = 4
    rhombusAreaPeri(d1, d2)
 
# This code is contributed
# by Surendra_Gangwar


C#
// C# program to calculate area
// and perimeter of a rhombus
// using diagonals
using System;
 
class GFG
{
 
// calculate area and perimeter
// of a rhombus
static int rhombusAreaPeri(int d1, 
                           int d2)
{
    int area, perimeter;
 
    area = (d1 * d2) / 2;
    perimeter = (int)(2 * Math.Sqrt(Math.Pow(d1, 2) +
                                    Math.Pow(d2, 2)));
 
    Console.WriteLine( "The area of rhombus with " +
                       "diagonals " + d1 + " and " +
                          d2 + " is " + area + ".");
 
    Console.WriteLine("The perimeter of rhombus " +
                      "with diagonals " + d1 + " and " +
                         d2 + " is " + perimeter + ".");
        return 0;
}
 
// Driver code
public static void Main ()
{
    int d1 = 2, d2 = 4;
    rhombusAreaPeri(d1, d2);
}
}
 
// This code is contributed by anuj_67..


PHP


Javascript


输出:
The area of rhombus with diagonals 2 and 4 is 4.
The perimeter of rhombus with diagonals 2 and 4 is 8.