Area of the circle that has a square and a circle inscribed in it - GeeksforGeeks

24 Feb, 2022
Given the side of a square a which is kept inside a circle. It keeps expanding until all four of its vertices touch the circumference of the circle. Another smaller circle is kept inside the square now and it keeps expanding until its circumference touches all the four sides of the square. The outer and the inner circle form a ring. Find the area of this shaded part as shown in the image below.  
Examples:  
Input: a = 3 Output: 7.06858Input: a = 4 Output: 12.566371  
 
Approach:  
From the above figure, R = a / sqrt(2) can be derived where a is the side length of the square. The area of the outer circle is (pi * R * R). 
Let s1 be the area of the outer circle (pi * R * R) and s2 be the area of the inner circle (pi * r * r). Then the area of the ring is s1 – s2.Below is the implementation of the above approach: 
C++
Java
Python3
C#
Javascript
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std; // Function to return the required areafloat getArea(int a){     // Calculate the area    float area = (M_PI * a * a) / 4.0;    return area;} // Driver codeint main(){    int a = 3;     cout << getArea(a);     return 0;}
// Java implementation of the approachclass GFG {     // Function to return the required area    static float getArea(int a)    {         // Calculate the area        float area = (float)(Math.PI * a * a) / 4;        return area;    }     // Driver code    public static void main(String args[])    {        int a = 3;        System.out.println(getArea(a));    }}
# Python3 implementation of the approachimport math # Function to return the required areadef getArea(a):         # Calculate the area    area = (math.pi * a * a) / 4    return area     # Driver codea = 3print('{0:.6f}'.format(getArea(a)))
// C# implementation of the approachusing System; class GFG{     // Function to return the required area    static float getArea(int a)    {         // Calculate the area        float area = (float)(Math.PI * a * a) / 4;        return area;    }     // Driver code    public static void Main()    {        int a = 3;        Console.Write(getArea(a));    }} // This code is contributed by mohit kumar 29
<script> // Javascript implementation of the approach // Function to return the required areafunction getArea(a){         // Calculate the area    var area = (Math.PI * a * a) / 4;    return area;} // Driver Codevar a = 3; document.write(getArea(a)); // This code is contributed by Kirti     </script>
7.06858
 
Time Complexity: O(1)
Auxiliary Space: O(1)
mohit kumar 29
Kirti_Mangal
subhammahato348
Analysis
Geometric
Mathematical
School Programming
Mathematical
Geometric
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Time Complexity and Space Complexity
Time Complexity of building a heap
NP-Completeness | Set 1 (Introduction)
Analysis of different sorting techniques
Analysis of Algorithms | Big-O analysis
Closest Pair of Points using Divide and Conquer algorithm
How to check if two given line segments intersect?
How to check if a given point lies inside or outside a polygon?
Find if two rectangles overlap
Convex Hull | Set 1 (Jarvis's Algorithm or Wrapping)