Wednesday, July 3, 2024
HomeData ModellingData Structure & AlgorithmCalculate area of pentagon with given diagonal

Calculate area of pentagon with given diagonal

Given an integer d which is the length of the diagonal of a pentagon, the task is to find the area of that pentagon. 
 

pentagon

Examples: 
 

Input: d = 5 
Output: 16.4291
Input: d = 10 
Output: 65.7164 
 

 

Approach: Pentagon is a regular polygon having five equal sides and all equal angles. The interior angles of pentagon are of 108 degrees each and the sum of all angles of a pentagon is 540 degrees. If d is the diagonal of the pentagon then it’s area is given by: 

    $$ \frac{1}{8} {d^2 (-5+ \sqrt{45})\sqrt{ \sqrt{20} + 5 } } $$

Below is the implementation of the above approach: 
 

C++




// C++ program to find the area of
// Pentagon with given diagonal
#include <bits/stdc++.h>
using namespace std;
 
// Function to return the area of the
// pentagon with diagonal d
float pentagonArea(float d)
{
    float area;
 
    // Formula to find area
    area = (d * d * (-5 + sqrt(45)) * sqrt(sqrt(20) + 5)) / 8;
 
    return area;
}
 
// Driver code
int main()
{
    float d = 5;
    cout << pentagonArea(d);
    return 0;
}


Java




// Java program to find the area of
// Pentagon with given diagonal
import java.text.*;
class GFG{
// Function to return the area of the
// pentagon with diagonal d
static double pentagonArea(double d)
{
    double area;
 
    // Formula to find area
    area = (d * d * (-5 + Math.sqrt(45)) * Math.sqrt(Math.sqrt(20) + 5)) / 8;
 
    return area;
}
 
// Driver code
public static void main(String[] args)
{
    double d = 5;
    DecimalFormat dec = new DecimalFormat("#0.0000");
    System.out.println(dec.format(pentagonArea(d)));
}
}
// This code is contributed by mits


Python3




# Python3 program to find the area of
# Pentagon with given diagonal
 
# from math lib import sqrt() method
from math import sqrt
 
# Function to return the area of the
# pentagon with diagonal d
def pentagonArea(d) :
 
    # Formula to find area
    area = (d * d * (-5 + sqrt(45)) * sqrt(sqrt(20) + 5)) / 8
 
    return round(area , 4)
  
 
# Driver code
if __name__ == "__main__" :
 
    d = 5
    print(pentagonArea(d))
 
# This code is contributed by Ryuga


C#




// C# program to find the area of
// Pentagon with given diagonal
using System;
 
class GFG{
// Function to return the area of the
// pentagon with diagonal d
static double pentagonArea(double d)
{
    double area;
 
    // Formula to find area
    area = (d * d * (-5 + Math.Sqrt(45)) * Math.Sqrt(Math.Sqrt(20) + 5)) / 8;
 
    return area;
}
 
// Driver code
public static void Main()
{
    double d = 5;
    Console.WriteLine("{0:F4}",pentagonArea(d));
}
}
// This code is contributed by mits


PHP




<?php
// PHP program to find the area of
// Pentagon with given diagonal
// Function to return the area of the
// pentagon with diagonal d
 Function pentagonArea($d)
{
    $area;
 
    // Formula to find area
    $area= ($d * $d * (-5 +sqrt(45)) * sqrt(sqrt(20) + 5)) / 8;
 
    return $area;
}
 
// Driver code
{
    $d = 5;
    echo(pentagonArea($d));
    return 0;
}
//This code is contributed by Mukul singh.


Javascript




<script>
// javascript program to find the area of
// Pentagon with given diagonal
 
// Function to return the area of the
// pentagon with diagonal d
function pentagonArea( d)
{
    let area;
 
    // Formula to find area
    area = (d * d * (-5 + Math.sqrt(45)) * Math.sqrt(Math.sqrt(20) + 5)) / 8;
    return area;
}
 
// Driver code
    let d = 5;
    document.write(pentagonArea(d).toFixed(4));
     
// This code is contributed by gauravrajput1
</script>


Output: 

16.4291

 

Time Complexity: O(1)

Auxiliary Space: O(1)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments