Diagonal of a Regular Pentagon
Last Updated :
25 Jun, 2022
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.32
Input: 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++
#include <bits/stdc++.h>
using namespace std;
float pentdiagonal( float a)
{
if (a < 0)
return -1;
float d = 1.22 * a;
return d;
}
int main()
{
float a = 6;
cout << pentdiagonal(a) << endl;
return 0;
}
|
Java
class GFG
{
static double pentdiagonal( double a)
{
if (a < 0 )
return - 1 ;
double d = 1.22 * a;
return d;
}
static public void main (String args[])
{
double a = 6 ;
System.out.println(pentdiagonal(a));
}
}
|
Python3
def pentdiagonal(a) :
if (a < 0 ) :
return - 1
d = 1.22 * a
return d
if __name__ = = "__main__" :
a = 6
print (pentdiagonal(a))
|
C#
using System;
public class GFG{
static double pentdiagonal( double a)
{
if (a < 0)
return -1;
double d = 1.22 * a;
return d;
}
static public void Main (){
double a = 6;
Console.WriteLine(pentdiagonal(a));
}
}
|
PHP
<?php
function pentdiagonal( $a )
{
if ( $a < 0)
return -1;
$d = 1.22 * $a ;
return $d ;
}
$a = 6;
echo pentdiagonal( $a );
?>
|
Javascript
<script>
function pentdiagonal(a)
{
if (a < 0)
return -1;
let d = 1.22 * a;
return d;
}
let a = 6;
document.write(pentdiagonal(a));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...