Diagonal of a Regular Pentagon - GeeksforGeeks

10 Mar, 2021
Given an integer a which is the side of a regular pentagon, the task is to find and print the length of its diagonal.  
Examples:  
Input: a = 6 Output: 7.32Input: a = 9 Output: 10.98  
 
Approach: We know that the sum of interior angles of a polygon = (n – 2) * 180 where, n is the no. of sides in the polygon. So, sum of interior angles of pentagon = 3 * 180 = 540 and each interior angle will be 108. Now, we have to find BC = 2 * x. If we draw a perpendicular AO on BC, we will see that the perpendicular bisects BC in BO and OC, as triangles AOB and AOC are congruent to each other. So, in triangle AOB, sin(54) = x / a i.e. x = 0.61 * a Therefore, diagonal length will be 2 * x i.e. 1.22 * a.Below is the implementation of the above approach: 
C++
Java
Python3
C#
PHP
Javascript
// C++ Program to find the diagonal// of a regular pentagon#include <bits/stdc++.h>using namespace std; // Function to find the diagonal// of a regular pentagonfloat pentdiagonal(float a){     // Side cannot be negative    if (a < 0)        return -1;     // Length of the diagonal    float d = 1.22 * a;    return d;} // Driver codeint main(){    float a = 6;    cout << pentdiagonal(a) << endl;    return 0;}
// Java Program to find the diagonal// of a regular pentagonclass GFG{     // Function to find the diagonal// of a regular pentagonstatic double pentdiagonal(double a){     // Side cannot be negative    if (a < 0)        return -1;     // Length of the diagonal    double d = 1.22 * a;    return d;} // Driver codestatic public void main (String args[]){    double a = 6;    System.out.println(pentdiagonal(a));}} // This code is contributed// by Akanksha Rai
# Python3 Program to find the diagonal# of a regular pentagon # Function to find the diagonal# of a regular pentagondef pentdiagonal(a) :     # Side cannot be negative    if (a < 0) :        return -1     # Length of the diagonal    d = 1.22 * a    return d # Driver codeif __name__ == "__main__" :     a = 6    print(pentdiagonal(a)) # This code is contributed by Ryuga
// C# Program to find the diagonal// of a regular pentagonusing System; public class GFG{     // Function to find the diagonal// of a regular pentagonstatic double pentdiagonal(double a){     // Side cannot be negative    if (a < 0)        return -1;     // Length of the diagonal    double d = 1.22 * a;    return d;} // Driver code    static public void Main (){    double a = 6;    Console.WriteLine(pentdiagonal(a));    }}
<?php// PHP Program to find the diagonal// of a regular pentagon // Function to find the diagonal// of a regular pentagonfunction pentdiagonal($a){     // Side cannot be negative    if ($a < 0)        return -1;     // Length of the diagonal    $d = 1.22 * $a;    return $d;} // Driver code$a = 6;echo pentdiagonal($a); // This code is contributed// by Sach_Code?>
<script>    // Javascript Program to find the diagonal    // of a regular pentagon         // Function to find the diagonal     // of a regular pentagon    function pentdiagonal(a)    {               // Side cannot be negative        if (a < 0)            return -1;               // Length of the diagonal        let d = 1.22 * a;        return d;    }         let a = 6;    document.write(pentdiagonal(a));     </script>
7.32
 
Sach_Code
ankthon
Akanksha_Rai
divyeshrabadiya07
C++ Programs
Geometric
Mathematical
Mathematical
Geometric
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Passing a function as a parameter in C++
Program to implement Singly Linked List in C++ using class
Const keyword in C++
cout in C++
Dynamic _Cast in C++
How to check if a given point lies inside or outside a polygon?
Closest Pair of Points using Divide and Conquer algorithm
Program for distance between two points on earth
How to check if two given line segments intersect?
Convex Hull | Set 1 (Jarvis's Algorithm or Wrapping)