Area of decagon inscribed within the circle
Last Updated :
09 Sep, 2023
Given here is a regular decagon, inscribed within a circle of radius r, the task is to find the area of the decagon.
Examples:
Input: r = 5
Output: 160.144
Input: r = 8
Output: 409.969
Approach:
We know, side of the decagon within the circle, a = r√(2-2cos36)(Refer here)
So, area of the decagon,
A = 5*a^2*(√5+2√5)/2 = 5 *(r√(2-2cos36))^2*(√5+2√5)/2=(5*r^2*(3-√5)*(√5+2√5))/4
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
float area( float r)
{
if (r < 0)
return -1;
float area = (5 * pow (r, 2) * (3 - sqrt (5))
* ( sqrt (5) + (2 * sqrt (5))))
/ 4;
return area;
}
int main()
{
float r = 8;
cout << area(r) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
static double area( double r)
{
if (r < 0 )
return - 1 ;
double area = ( 5 * Math.pow(r, 2 ) * ( 3 - Math.sqrt( 5 ))
* (Math.sqrt( 5 ) + (( 2 * Math.sqrt( 5 ))))/ 4 );
return area;
}
public static void main (String[] args) {
double r = 8 ;
System.out.println (area(r));
}
}
|
Python3
from math import sqrt, pow
def area(r):
if r < 0 :
return - 1
area = ( 5 * pow (r, 2 ) * ( 3 - sqrt( 5 )) *
(sqrt( 5 ) + ( 2 * sqrt( 5 )))) / 4
return area
if __name__ = = '__main__' :
r = 8
print (area(r))
|
C#
using System;
class GFG
{
static double area( double r)
{
if (r < 0)
return -1;
double area = (5 * Math.Pow(r, 2) *
(3 - Math.Sqrt(5)) *
(Math.Sqrt(5) +
((2 * Math.Sqrt(5))))/ 4);
return area;
}
static public void Main ()
{
double r = 8;
Console.WriteLine (area(r));
}
}
|
Javascript
<script>
function area( r)
{
if (r < 0)
return -1;
var area = (5 * Math.pow(r, 2) * (3 - Math.sqrt(5))
* (Math.sqrt(5) + ((2 * Math.sqrt(5))))/ 4);
return area;
}
var r = 8;
document.write(area(r).toFixed(3));
</script>
|
PHP
<?php
function area( $r )
{
if ( $r < 0)
return -1;
$area = (5 * pow( $r , 2) * (3 - sqrt(5)) *
(sqrt(5) + (2 * sqrt(5)))) / 4;
return $area ;
}
$r = 8;
echo area( $r ) . "\n" ;
?>
|
Time complexity: O(1)
Auxiliary Space: O(1), since no extra space has been taken.
Share your thoughts in the comments
Please Login to comment...