给定整数a ,它是规则七边形的边,任务是查找并打印其对角线的长度。
例子:
Input: a = 6
Output: 10.812
Input: a = 9
Output: 16.218
方法:我们知道多边形的内角总和= (n – 2)* 180 ,其中, n为否。多边形中的边数。
因此,七边形的内角之和= 5 * 180 = 900 ,每个内角将为128.58 (大约)。
现在,我们必须找到BC = 2 * x 。如果在BC上绘制垂直AO ,我们将看到BO和OC中的垂直二等分BC ,因为三角形AOB和AOC彼此相等。
因此,在三角形AOB中, sin(64.29)= x / a即x = 0.901 * a
因此,对角线长度将为2 * x,即1.802 * a 。
下面是上述方法的实现:
C++
// C++ Program to find the diagonal
// of a regular heptagon
#include
using namespace std;
// Function to return the diagonal
// of a regular heptagon
float heptdiagonal(float a)
{
// Side cannot be negative
if (a < 0)
return -1;
// Length of the diagonal
float d = 1.802 * a;
return d;
}
// Driver code
int main()
{
float a = 6;
cout << heptdiagonal(a) << endl;
return 0;
}
Java
// Java program to find the diagonal of a regular heptagon
import java.util.*;
import java.lang.*;
import java.io.*;
public class GFG {
// Function to return the diagonal of a regular heptagon
static double heptdiagonal(double a)
{
//side cannot be negative
if(a<0)
return -1;
// length of the diagonal
double d=1.802*a;
return d;
}
// Driver code
public static void main(String[] args)
{
int a = 6;
System.out.println(heptdiagonal(a));
}
}
Python3
# Python3 Program to find the diagonal
# of a regular heptagon
# Function to return the diagonal
# of a regular heptagon
def heptdiagonal(a) :
# Side cannot be negative
if (a < 0) :
return -1
# Length of the diagonal
d = 1.802 * a
return round(d, 3)
# Driver code
if __name__ == "__main__" :
a = 6
print(heptdiagonal(a))
# This code is contributed by Ryuga
C#
// C# program to find the diagonal of a regular heptagon
using System;
public class GFG {
// Function to return the diagonal of a regular heptagon
static double heptdiagonal(double a)
{
//side cannot be negative
if(a<0)
return -1;
// length of the diagonal
double d=1.802*a;
return d;
}
// Driver code
public static void Main()
{
int a = 6;
Console.WriteLine(heptdiagonal(a));
}
} // This code is contributed by Mukul singh
PHP
Javascript
输出:
10.812