Find the area of the shaded region formed by the intersection of four semicircles in a square

Given the length of the side of a square a, the task is to find the area of the shaded region formed by the intersection of four semicircles in a square as shown in the image below:Â
Â
Examples:Â
Â
Input: a = 10Â
Output: 57
Input: a = 19Â
Output: 205.77Â
Â
Â
Approach: Area of the shaded region will be:Â
Â
Area(semicircle1) + Area(semicircle2) + Area(semicircle3) + Area(semicircle4) – Area(square).
Since all semicircles are of same radius, therefore, area of all semicircles will be equal. So, the above formula can be written as:Â
4*(Area of Semicircle) – Area(Square)Â
Â
The area of a semicircle is (3.14 * r2) / 2 where r is the radius of the semicircle which is equal to a / 2.
Hence, Area of the shaded region = 4 * (3.14 * (a * a) / 8 ) – a * a
Â
Below is the implementation of the above approach:
Â
C++
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std;Â
// Function to return the area// of the shaded regionfloat findAreaShaded(float a){Â
    // Area of the square    float sqArea = a * a;Â
    // Area of the semicircle    float semiCircleArea = (3.14 * (a * a) / 8);Â
    // There are 4 semicircles    // shadedArea = Area of 4 semicircles - Area of square    float ShadedArea = 4 * semiCircleArea - sqArea;Â
    return ShadedArea;}Â
// Driver codeint main(){Â Â Â Â float a = 10;Â Â Â Â cout << findAreaShaded(a);Â
    return 0;} |
Java
// Java implementation of the approachclass GFG {Â
    // Function to return the area    // of the shaded region    static float findAreaShaded(float a)    {Â
        // Area of the square        float sqArea = a * a;Â
        // Area of the semicircle        float semiCircleArea = (float)(3.14 * (a * a) / 8);Â
        // There are 4 semicircles        // shadedArea = Area of 4 semicircles - Area of square        float ShadedArea = 4 * semiCircleArea - sqArea;Â
        return ShadedArea;    }Â
    // Driver code    public static void main(String[] args)    {        float a = 10;        System.out.println(findAreaShaded(a));    }} |
Python3
# Python3 implementation of the approachÂ
# Function to return the area # of the shaded regiondef findAreaShaded(a):         # Area of the square    sqArea = a * a;Â
    # Area of the semicircle    semiCircleArea = (3.14 * (a * a ) / 8)Â
    # There are 4 semicircles    # shadedArea = Area of 4 semicircles - Area of square    ShadedArea = 4 * semiCircleArea - sqArea ; Â
    return ShadedArea;Â
# Driver codeif __name__ == '__main__':Â Â Â Â a = 10Â Â Â Â print(findAreaShaded(a)) |
C#
// C# implementation of the approachusing System;Â
class GFG {Â
    // Function to return the area    // of the shaded region    static float findAreaShaded(float a)    {Â
        // Area of the square        float sqArea = a * a;Â
        // Area of the semicircle        float semiCircleArea = (float)(3.14 * (a * a) / 8);Â
        // There are 4 semicircles        // shadedArea = Area of 4 semicircles - Area of square        float ShadedArea = 4 * semiCircleArea - sqArea;Â
        return ShadedArea;    }Â
    // Driver code    public static void Main()    {        float a = 10;        Console.WriteLine(findAreaShaded(a));    }}Â
// This code is contributed by mohit kumar 29 |
PHP
<?php// PHP implementation of the approach Â
// Function to return the area // of the shaded region function findAreaShaded($a) { Â
    // Area of the square     $sqArea = $a * $a; Â
    // Area of the semicircle     $semiCircleArea = (3.14 * ($a * $a) / 8); Â
    // There are 4 semicircles     // shadedArea = Area of 4 semicircles -     //             Area of square     $ShadedArea = 4 * $semiCircleArea - $sqArea; Â
    return $ShadedArea; } Â
// Driver code $a = 10; echo findAreaShaded($a); Â
// This code is contributed by Ryuga?> |
Javascript
<script>Â
// Javascript implementation of the approachÂ
// Function to return the area// of the shaded regionfunction findAreaShaded( a){Â
    // Area of the square    let sqArea = a * a;Â
    // Area of the semicircle    let semiCircleArea = (3.14 * (a * a) / 8);Â
    // There are 4 semicircles    // shadedArea = Area of 4 semicircles - Area of square    let ShadedArea = 4 * semiCircleArea - sqArea;Â
    return ShadedArea;}Â
Â
    // driver code Â
    let a = 10;    document.write(findAreaShaded(a));     </script> |
57
Â
Time Complexity: O(1)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!




