Given a n-sided polygon with side length a. The task is to find the area of the circumcircle of the polygon.
Examples:
Input: n = 10, a = 3 Output: 1.99737
Input: n = 5, a = 6 Output: 3.02487
Approach: A regular n-gon divides the circle into n pieces, so the central angle of the triangle is a full circle divided by n: 360 deg/n.Â
Applying the law of cosines for the three side lengths of the triangle, we get
c2 = a2 + b2 - 2ab cos C or, a2 = r2 + r2 - 2rr cos (360/n) or, a2 = 2r2 - 2r2 cos (360/n) or, c2 = r2 (2 - 2 cos (360/n)) so, a=r?(2-2cos(360/n)) Therefore, r=a/?(2-2cos(360/n))Â
Below is the implementation of the above approach:Â
C++
// C++ Program to find the radius // of the circumcircle of the given polygon Â
#include <bits/stdc++.h> using namespace std; Â
// Function to find the radius // of the circumcircle float findRadiusOfcircumcircle( float n, float a) { Â
    // these cannot be negative     if (n < 0 || a < 0)         return -1; Â
    // Radius of the circumcircle     float radius = a / sqrt (2 - (2 * cos (360 / n))); Â
    // Return the radius     return radius; } Â
// Driver code int main() { Â
    float n = 5, a = 6; Â
    // Find the radius of the circumcircle     cout << findRadiusOfcircumcircle(n, a) << endl; Â
    return 0; } |
Java
// Java Program to find the radius // of the circumcircle of the given polygon Â
import java.io.*; Â
class GFG { Â Â Â // Function to find the radius // of the circumcircle static float findRadiusOfcircumcircle( float n, float a) { Â
    // these cannot be negative     if (n < 0 || a < 0 )         return - 1 ; Â
    // Radius of the circumcircle     float radius = ( float )(a / Math.sqrt( 2 - ( 2 * Math.cos( 360 / n)))); Â
    // Return the radius     return radius; } Â
// Driver code Â
    public static void main (String[] args) {         float n = 5 , a = 6 ; Â
    // Find the radius of the circumcircle     System.out.println( findRadiusOfcircumcircle(n, a)) ; Â
    } } // This code is contributed // by anuj_67.. |
Python 3
# Python3 Program to find the # radius of the circumcircle # of the given polygon Â
# from math import all methods from math import * Â
# Function to find the radius # of the circumcircle def findRadiusOfcircumcircle(n, a) : Â
    # these cannot be negative     if n < 0 or a < 0 :         return - 1 Â
    # Radius of the circumcircle     radius = a / sqrt( 2 - ( 2 * cos( 360 / n))) Â
    # Return the radius     return radius Â
# Driver code if __name__ = = "__main__" : Â
    n , a = 5 , 6 Â
    # Find the radius of the circumcircle     print ( round (findRadiusOfcircumcircle(n, a), 5 )) Â
# This code is contributed # by ANKITRAI1 |
C#
// C# Program to find the radius // of the circumcircle of the given polygon using System; Â
class GFG { Â
// Function to find the radius // of the circumcircle static float findRadiusOfcircumcircle( float n, Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â float a) { Â
    // these cannot be negative     if (n < 0 || a < 0)         return -1; Â
    // Radius of the circumcircle     float radius = ( float )(a / Math.Sqrt(2 -                    (2 * Math.Cos(360 / n)))); Â
    // Return the radius     return radius; } Â
// Driver code public static void Main () { Â Â Â Â float n = 5, a = 6; Â
    // Find the radius of the circumcircle     Console.WriteLine(findRadiusOfcircumcircle(n, a)); } } Â
// This code is contributed // by anuj_67 |
PHP
<?php // PHP Program to find the radius // of the circumcircle of the // given polygon Â
// Function to find the radius // of the circumcircle function findRadiusOfcircumcircle( $n , $a ) { Â
    // these cannot be negative     if ( $n < 0 || $a < 0)         return -1; Â
    // Radius of the circumcircle     $radius = $a / sqrt(2 - (2 *                     cos (360 / $n ))); Â
    // Return the radius     return $radius ; } Â
// Driver code $n = 5; $a = 6; Â
// Find the radius of the circumcircle echo findRadiusOfcircumcircle( $n , $a ); Â
// This code is contributed by Anuj_67.. ?> |
Javascript
<script> // javascript Program to find the radius // of the circumcircle of the given polygon Â
// Function to find the radius // of the circumcircle function findRadiusOfcircumcircle(n , a) { Â
    // these cannot be negative     if (n < 0 || a < 0)         return -1; Â
    // Radius of the circumcircle     var radius = (a / Math.sqrt(2 - (2 * Math.cos(360 / n)))); Â
    // Return the radius     return radius; } Â
// Driver code var n = 5, a = 6; Â
// Find the radius of the circumcircle document.write( findRadiusOfcircumcircle(n, a).toFixed(5)) ; Â
Â
// This code is contributed by shikhasingrajput </script> |
3.02487
Â
Time Complexity : O(log(n)) because it is using inbuilt sqrt function
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!