给定一个正五边形边的整数a ,任务是找到并打印其对角线的长度。
例子:
Input: a = 6
Output: 7.32
Input: a = 9
Output: 10.98
方法:我们知道多边形的内角和 = (n – 2) * 180其中, n是第一个。多边形中的边数。
因此,五边形的内角和 = 3 * 180 = 540并且每个内角将是108 。
现在,我们必须找到BC = 2 * x 。如果我们在BC上画一条垂直的AO ,我们将看到BO和OC 中的BC的垂直平分线,因为三角形AOB和AOC彼此全等。
所以,在三角形AOB 中, sin(54) = x / a即x = 0.61 * a
因此,对角线长度将为2 * x即1.22 * a 。
下面是上述方法的实现:
C++
// C++ Program to find the diagonal
// of a regular pentagon
#include
using namespace std;
// Function to find the diagonal
// of a regular pentagon
float pentdiagonal(float a)
{
// Side cannot be negative
if (a < 0)
return -1;
// Length of the diagonal
float d = 1.22 * a;
return d;
}
// Driver code
int main()
{
float a = 6;
cout << pentdiagonal(a) << endl;
return 0;
}
Java
// Java Program to find the diagonal
// of a regular pentagon
class GFG
{
// Function to find the diagonal
// of a regular pentagon
static 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 (String args[])
{
double a = 6;
System.out.println(pentdiagonal(a));
}
}
// This code is contributed
// by Akanksha Rai
Python3
# Python3 Program to find the diagonal
# of a regular pentagon
# Function to find the diagonal
# of a regular pentagon
def pentdiagonal(a) :
# Side cannot be negative
if (a < 0) :
return -1
# Length of the diagonal
d = 1.22 * a
return d
# Driver code
if __name__ == "__main__" :
a = 6
print(pentdiagonal(a))
# This code is contributed by Ryuga
C#
// C# Program to find the diagonal
// of a regular pentagon
using System;
public class GFG{
// Function to find the diagonal
// of a regular pentagon
static 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
Javascript
输出:
7.32
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。