Largest rectangle that can be inscribed in a semicircle - GeeksforGeeks

15 Mar, 2021
Given a semicircle of radius r, we have to find the largest rectangle that can be inscribed in the semicircle, with base lying on the diameter.Examples:  
Input : r = 4
Output : 16

Input : r = 5 
Output :25
 
 
Let r be the radius of the semicircle, x one half of the base of the rectangle, and y the height of the rectangle. We want to maximize the area, A = 2xy. So from the diagram we have, y = √(r^2 – x^2) So, A = 2*x*(√(r^2 – x^2)), or dA/dx = 2*√(r^2 – x^2) -2*x^2/√(r^2 – x^2) Setting this derivative equal to 0 and solving for x, dA/dx = 0 or, 2*√(r^2 – x^2) – 2*x^2/√(r^2 – x^2) = 0 2r^2 – 4x^2 = 0 x = r/√2This is the maximum of the area as, dA/dx > 0 when x > r/√2 and, dA/dx < 0 when x > r/√2Since y =√(r^2 – x^2) we then havey = r/√2Thus, the base of the rectangle has length = r/√2 and its height has length √2*r/2. So, Area, A=r^2 
C++
Java
Python 3
C#
PHP
Javascript
// C++ Program to find the// the biggest rectangle// which can be inscribed// within the semicircle#include <bits/stdc++.h>using namespace std; // Function to find the area// of the biggest rectanglefloat rectanglearea(float r){     // the radius cannot be negative    if (r < 0)        return -1;     // area of the rectangle    float a = r * r;     return a;} // Driver codeint main(){    float r = 5;    cout << rectanglearea(r) << endl;    return 0;}
// Java Program to find the// the biggest rectangle// which can be inscribed// within the semicircleclass GFG{ // Function to find the area// of the biggest rectanglestatic float rectanglearea(float r){ // the radius cannot be negativeif (r < 0)    return -1; // area of the rectanglefloat a = r * r; return a;} // Driver codepublic static void main(String[] args){    float r = 5;    System.out.println((int)rectanglearea(r));}} // This code is contributed// by ChitraNayal
# Python 3 Program to find the# the biggest rectangle# which can be inscribed# within the semicircle # Function to find the area# of the biggest rectangledef rectanglearea(r) :     # the radius cannot    # be negative    if r < 0 :        return -1     # area of the rectangle    a = r * r     return a # Driver Codeif __name__ == "__main__" :     r = 5     # function calling    print(rectanglearea(r)) # This code is contributed# by ANKITRAI1
// C# Program to find the// the biggest rectangle// which can be inscribed// within the semicircleusing System; class GFG{ // Function to find the area// of the biggest rectanglestatic float rectanglearea(float r){ // the radius cannot be negativeif (r < 0)    return -1; // area of the rectanglefloat a = r * r; return a;} // Driver codepublic static void Main(){    float r = 5;    Console.Write((int)rectanglearea(r));}} // This code is contributed// by ChitraNayal
<?php// PHP Program to find the// the biggest rectangle// which can be inscribed// within the semicircle // Function to find the area// of the biggest rectanglefunction rectanglearea($r){     // the radius cannot    // be negative    if ($r < 0)        return -1;     // area of the rectangle    $a = $r * $r;     return $a;} // Driver code$r = 5;echo rectanglearea($r)."\n"; // This code is contributed// by ChitraNayal?>
<script> // javascript Program to find the// the biggest rectangle// which can be inscribed// within the semicircle // Function to find the area// of the biggest rectanglefunction rectanglearea(r){     // the radius cannot be negative    if (r < 0)        return -1;         // area of the rectangle    var a = r * r;         return a;} // Driver code var r = 5;document.write(parseInt(rectanglearea(r))); // This code is contributed by Amit Katiyar </script>
OUTPUT :  
25
 
ankthon
ukasp
amit143katiyar
circle
square-rectangle
Geometric
School Programming
Geometric
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Convex Hull | Set 2 (Graham Scan)
Check whether a given point lies inside a triangle or not
Convex Hull using Divide and Conquer Algorithm
Optimum location of point to minimize total distance
Program to find area of a triangle
Python Dictionary
Arrays in C/C++
Inheritance in C++
Reverse a string in Java
C++ Classes and Objects