Largest square that can be inscribed in a semicircle

09 Sep, 2021
Given a semicircle with radius r, we have to find the largest square that can be inscribed in the semicircle, with base lying on the diameter.
Examples: 
Input: r = 5
Output: 20

Input: r = 8
Output: 51.2
Approach: Let r be the radius of the semicircle & a be the side length of the square. From the figure we can see that, centre of the circle is also the midpoint of the base of the square. So in the right angled triangle AOB, from Pythagoras Theorem:
a^2 + (a/2)^2 = r^25*(a^2/4) = r^2a^2 = 4*(r^2/5) i.e. area of the square 
Below is the implementation of the above approach:  
C++
Java
Python3
C#
PHP
Javascript
// C++ Program to find the biggest square// which can be inscribed within the semicircle#include <bits/stdc++.h>using namespace std; // Function to find the area// of the squarefloat squarearea(float r){     // the radius cannot be negative    if (r < 0)        return -1;     // area of the square    float a = 4 * (pow(r, 2) / 5);     return a;} // Driver codeint main(){    float r = 5;    cout << squarearea(r) << endl;     return 0;}
// Java Program to find the biggest square// which can be inscribed within the semicircle import java.io.*; class GFG {  // Function to find the area// of the squarestatic float squarearea(float r){     // the radius cannot be negative    if (r < 0)        return -1;     // area of the square    float a = 4 * (float)(Math.pow(r, 2) / 5);     return a;} // Driver code     public static void main (String[] args) {         float r = 5;    System.out.println( squarearea(r));    }}// This code is contributed by chandan_jnu.
# Python 3 program to find the# biggest square which can be# inscribed within the semicircle # Function to find the area# of the squaredef squarearea(r):     # the radius cannot be    # negative    if (r < 0):        return -1     # area of the square    a = 4 * (pow(r, 2) / 5)     return a # Driver codeif __name__ == "__main__":         r = 5    print(int(squarearea(r))) # This code is contributed# by ChitraNayal
// C# Program to find the// biggest square which can be// inscribed within the semicircleusing System; class GFG{ // Function to find the// area of the squarestatic float squarearea(float r){     // the radius cannot be negative    if (r < 0)        return -1;     // area of the square    float a = 4 * (float)(Math.Pow(r, 2) / 5);     return a;} // Driver codepublic static void Main (){    float r = 5;    Console.WriteLine(squarearea(r));}} // This code is contributed// by anuj_67
<?php// PHP Program to find the// biggest square which can be// inscribed within the semicircle // Function to find the area// of the squarefunction squarearea($r){     // the radius cannot be negative    if ($r < 0)        return -1;     // area of the square    $a = 4 * (pow($r, 2) / 5);     return $a;} // Driver code$r = 5;echo squarearea($r); // This code is contributed// by Shivi_Aggarwal?>
<script> // javascript Program to find the biggest square// which can be inscribed within the semicircle // Function to find the area// of the squarefunction squarearea(r){     // the radius cannot be negative    if (r < 0)        return -1;     // area of the square    var a = 4 * (Math.pow(r, 2) / 5);     return a;} // Driver codevar r = 5;document.write( squarearea(r)); // This code contributed by Princi Singh </script>
20
 
Chandan_Kumar
Shivi_Aggarwal
vt_m
ukasp
Akanksha_Rai
princi singh
gulshankumarar231
surindertarika1234
circle
square-rectangle
Geometric
Mathematical
Mathematical
Geometric
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Optimum location of point to minimize total distance
Line Clipping | Set 1 (Cohen–Sutherland Algorithm)
Check whether a given point lies inside a triangle or not
Program for Point of Intersection of Two Lines
Window to Viewport Transformation in Computer Graphics with Implementation
Program for Fibonacci numbers
Set in C++ Standard Template Library (STL)
Write a program to print all permutations of a given string
C++ Data Types
Merge two sorted arrays