📜  圆内刻有十边形的面积

📅  最后修改于: 2021-04-22 05:51:44             🧑  作者: Mango

给定一个正则十边形,刻在半径r的圆内,任务是找到十边形的面积。
例子:

Input: r = 5
Output: 160.144

Input: r = 8
Output: 409.969

方法
我们知道,在圆内的十边形边上, a =r√(2-2cos36) (请参阅此处)
因此,十边形的面积

下面是上述方法的实现:

C++
// C++ Program to find the area of the decagon
// inscribed within a circle
#include 
using namespace std;
 
// Function to find the area of the decagon
float area(float r)
{
 
    // radius cannot be negative
    if (r < 0)
        return -1;
 
    // area of the decagon
    float area = (5 * pow(r, 2) * (3 - sqrt(5))
                  * (sqrt(5) + (2 * sqrt(5))))
                 / 4;
    return area;
}
 
// Driver code
int main()
{
    float r = 8;
    cout << area(r) << endl;
 
    return 0;
}
Java // Java Program to find the area of the decagon 
// inscribed within a circle 

import java.io.*;

class GFG {
    
// Function to find the area of the decagon 
static double area(double  r) 
{ 

    // radius cannot be negative 
    if (r < 0) 
        return -1; 

    // area of the decagon 
    double  area = (5 * Math.pow(r, 2) * (3 - Math.sqrt(5)) 
                * (Math.sqrt(5) + ((2 * Math.sqrt(5))))/ 4); 
    return area; 
} 

// Driver code 
    
    public static void main (String[] args) {
        double  r = 8; 
        System.out.println (area(r)); 
    }
//This code is contributed by ajit
}


Python3
# Python3 Program to find the area of
# the decagon inscribed within a circle
from math import sqrt,pow
 
# Function to find the
# area of the decagon
def area(r):
     
    # radius cannot be negative
    if r < 0:
        return -1
 
    # area of the decagon
    area = (5 * pow(r, 2) * (3 - sqrt(5)) *
                 (sqrt(5) + (2 * sqrt(5))))/ 4
    return area
 
# Driver code
if __name__ == '__main__':
    r = 8
    print(area(r))
 
# This code is contributed
# by Surendra_Gangwar


C#
// C# Program to find the area of the
// decagon inscribed within a circle
using System;
 
class GFG
{
         
// Function to find the area
// of the decagon
static double area(double r)
{
 
    // radius cannot be negative
    if (r < 0)
        return -1;
 
    // area of the decagon
    double area = (5 * Math.Pow(r, 2) *
                  (3 - Math.Sqrt(5)) *
                      (Math.Sqrt(5) +
                 ((2 * Math.Sqrt(5))))/ 4);
    return area;
}
 
// Driver code
static public void Main ()
{
    double r = 8;
    Console.WriteLine (area(r));
}
}
 
// This code is contributed by akt_mit


PHP


Javascript


输出:
409.969