Diagonal of a Regular Heptagon
Last Updated :
25 Jun, 2022
Given an integer a which is the side of a regular heptagon, the task is to find and print the length of its diagonal.
Examples:
Input: a = 6
Output: 10.812
Input: a = 9
Output: 16.218
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 heptagon = 5 * 180 = 900 and each interior angle will be 128.58(Approx).
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(64.29) = x / a i.e. x = 0.901 * a
Therefore, diagonal length will be 2 * x i.e. 1.802 * a.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
float heptdiagonal( float a)
{
if (a < 0)
return -1;
float d = 1.802 * a;
return d;
}
int main()
{
float a = 6;
cout << heptdiagonal(a) << endl;
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
import java.io.*;
public class GFG {
static double heptdiagonal( double a)
{
if (a< 0 )
return - 1 ;
double d= 1.802 *a;
return d;
}
public static void main(String[] args)
{
int a = 6 ;
System.out.println(heptdiagonal(a));
}
}
|
Python3
def heptdiagonal(a) :
if (a < 0 ) :
return - 1
d = 1.802 * a
return round (d, 3 )
if __name__ = = "__main__" :
a = 6
print (heptdiagonal(a))
|
C#
using System;
public class GFG {
static double heptdiagonal( double a)
{
if (a<0)
return -1;
double d=1.802*a;
return d;
}
public static void Main()
{
int a = 6;
Console.WriteLine(heptdiagonal(a));
}
}
|
PHP
<?php
function heptdiagonal( $a )
{
if ( $a < 0)
return -1;
$d = 1.802 * $a ;
return $d ;
}
$a = 6;
echo heptdiagonal( $a );
|
Javascript
<script>
function heptdiagonal(a)
{
if (a < 0)
return -1;
var d = 1.802*a;
return d;
}
var a = 6;
document.write(heptdiagonal(a).toFixed(5));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...