The biggest possible circle that can be inscribed in a rectangle - GeeksforGeeks

31 Aug, 2021
Given a rectangle of length l & breadth b, we have to find the largest circle that can be inscribed in the rectangle. Examples:  
Input  : l = 4, b = 8
Output : 12.56

Input  : l = 16 b = 6
Output : 28.26
 
 
From the figure, we can see, the biggest circle that could be inscribed in the rectangle will have radius always equal to the half of the shorter side of the rectangle. So from the figure, 
radius, r = b/2 & Area, A = π * (r^2)
 
C++
Java
Python 3
C#
PHP
Javascript
// C++ Program to find the biggest circle// which can be inscribed  within the rectangle#include <bits/stdc++.h>using namespace std; // Function to find the area// of the biggest circlefloat circlearea(float l, float b){     // the length and breadth cannot be negative    if (l < 0 || b < 0)        return -1;     // area of the circle    if (l < b)        return 3.14 * pow(l / 2, 2);    else        return 3.14 * pow(b / 2, 2);} // Driver codeint main(){    float l = 4, b = 8;    cout << circlearea(l, b) << endl;    return 0;}
// Java Program to find the// biggest circle which can be// inscribed within the rectangle class GFG{ // Function to find the area// of the biggest circlestatic float circlearea(float l,                        float b){ // the length and breadth// cannot be negativeif (l < 0 || b < 0)    return -1; // area of the circleif (l < b)    return (float)(3.14 * Math.pow(l / 2, 2));else    return (float)(3.14 * Math.pow(b / 2, 2));} // Driver codepublic static void main(String[] args){    float l = 4, b = 8;    System.out.println(circlearea(l, b));}} // This code is contributed// by ChitraNayal
# Python 3 Program to find the# biggest circle which can be# inscribed within the rectangle # Function to find the area# of the biggest circledef circlearea(l, b):     # the length and breadth    # cannot be negative    if (l < 0 or b < 0):        return -1     # area of the circle    if (l < b):        return 3.14 * pow(l // 2, 2)    else:        return 3.14 * pow(b // 2, 2) # Driver codeif __name__ == "__main__":    l = 4    b = 8    print(circlearea(l, b)) # This code is contributed# by ChitraNayal
// C# Program to find the// biggest circle which can be// inscribed within the rectangleusing System; class GFG{ // Function to find the area// of the biggest circlestatic float circlearea(float l,                        float b){ // the length and breadth// cannot be negativeif (l < 0 || b < 0)    return -1; // area of the circleif (l < b)    return (float)(3.14 * Math.Pow(l / 2, 2));else    return (float)(3.14 * Math.Pow(b / 2, 2));} // Driver codepublic static void Main(){    float l = 4, b = 8;    Console.Write(circlearea(l, b));}} // This code is contributed// by ChitraNayal
<?php// PHP Program to find the// biggest circle which can be// inscribed within the rectangle // Function to find the area// of the biggest circlefunction circlearea($l, $b){     // the length and breadth    // cannot be negative    if ($l < 0 || $b < 0)        return -1;     // area of the circle    if ($l < $b)        return 3.14 * pow($l / 2, 2);    else        return 3.14 * pow($b / 2, 2);} // Driver code$l = 4;$b = 8;echo circlearea($l, $b)."\n"; // This code is contributed// by ChitraNayal?>
<script> // javascript Program to find the// biggest circle which can be// inscribed within the rectangle // Function to find the area// of the biggest circlefunction circlearea(l, b){     // the length and breadth    // cannot be negative    if (l < 0 || b < 0)        return -1;         // area of the circle    if (l < b)        return (3.14 * Math.pow(l / 2, 2));    else        return (3.14 * Math.pow(b / 2, 2));} // Driver code var l = 4, b = 8;document.write(circlearea(l, b)); // This code is contributed by Amit Katiyar </script>
12.56
 
ukasp
Akanksha_Rai
amit143katiyar
rs1686740
circle
square-rectangle
Geometric
Geometric
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Program for distance between two points on earth
Convex Hull | Set 1 (Jarvis's Algorithm or Wrapping)
Closest Pair of Points | O(nlogn) Implementation
Line Clipping | Set 1 (Cohen–Sutherland Algorithm)
Convex Hull | Set 2 (Graham Scan)
Given n line segments, find if any two segments intersect
Sum of Manhattan distances between all pairs of points
Polygon Clipping | Sutherland–Hodgman Algorithm
Convex Hull using Divide and Conquer Algorithm
Program for Point of Intersection of Two Lines