📜  八边形对角线的长度

📅  最后修改于: 2021-04-24 03:28:55             🧑  作者: Mango

这里给出的是边长为a的正八边形,任务是找到长度的它的对角线。
例子:

Input: a = 4
Output: 10.4525

Input: a = 5
Output: 13.0656

方法:从图中可以清楚地看出,

下面是上述方法的实现:

C++
// C++ Program to find the diagonal
// of the octagon
#include 
using namespace std;
 
// Function to find the diagonal
// of the octagon
float octadiagonal(float a)
{
 
    // side cannot be negative
    if (a < 0)
        return -1;
 
    // diagonal of the octagon
    return a * sqrt(4 + (2 * sqrt(2)));
}
 
// Driver code
int main()
{
    float a = 4;
    cout << octadiagonal(a) << endl;
 
    return 0;
}


Java
// Java  Program to find the diagonal
// of the octagon
import java.util.*;
class solution
{
   
// Function to find the diagonal
// of the octagon
static double octadiagonal(double a)
{
   
    // side cannot be negative
    if (a < 0)
        return -1;
   
    // diagonal of the octagon
    return a * Math.sqrt(4 + (2 * Math.sqrt(2)));
}
   
// Driver code
public static void main(String args[])
{
    double a = 4;
    System.out.println( octadiagonal(a));
   
}
}
//contributed by Arnab Kundu


Python3
# Python3 Program to find the diagonal
# of the octagon
import math
# Function to find the diagonal
# of the octagon
def octadiagonal(a):
 
    # side cannot be negative
    if (a < 0):
        return -1;
 
    # diagonal of the octagon
    return a * math.sqrt(4 + (2 * math.sqrt(2)))
 
 
# Driver code
if __name__=='__main__':
    a = 4
    print (octadiagonal(a))
 
# This code is contributed by
# Shivi_Aggarwal


C#
// C# Program to find the diagonal
// of the octagon
using System;
 
class GFG
{
 
// Function to find the diagonal
// of the octagon
static double octadiagonal(double a)
{
 
    // side cannot be negative
    if (a < 0)
        return -1;
 
    // diagonal of the octagon
    return a * Math.Sqrt(4 +
          (2 * Math.Sqrt(2)));
}
 
// Driver code
public static void Main()
{
    double a = 4;
    Console.WriteLine(octadiagonal(a));
}
}
 
// This code is contributed
// by inder_verma


PHP


Javascript


输出:
10.4525