Circumradius of the rectangle - GeeksforGeeks

08 Mar, 2022
Here we have a rectangle of length l & breadth b. We have to find the circumradius of the rectangle.
Examples: 
Input : l = 3, b = 4 
Output :2.5

Input :l = 10, b = 12
Output :3.95227774224
Approach: From the diagram, we can clearly understand the circumradius r is half of the diagonal of the rectangle. 
r = √(l^2 + b^2)/2
Below is the implementation of the above approach: 
C++
C
Java
Python3
C#
PHP
Javascript
// C++ Program to find the radius// of the circumcircle of the given rectangle #include <bits/stdc++.h>using namespace std; // Function to find the radius// of the circumcirclefloat findRadiusOfcircumcircle(float l, float b){     // the sides cannot be negative    if (l < 0 || b < 0)        return -1;     // Radius of the circumcircle    float radius = sqrt(pow(l, 2) + pow(b, 2)) / 2;     // Return the radius    return radius;} // Driver codeint main(){     // Get the sides of the triangle    float l = 4, b = 3;    // Find the radius of the circumcircle    cout << findRadiusOfcircumcircle(l, b) << endl;     return 0;}
// C Program to find the radius// of the circumcircle of the given rectangle#include <stdio.h>#include <math.h> // Function to find the radius// of the circumcirclefloat findRadiusOfcircumcircle(float l, float b){       // the sides cannot be negative    if (l < 0 || b < 0)        return -1;       // Radius of the circumcircle    float radius = sqrt(pow(l, 2) + pow(b, 2)) / 2;      // Return the radius    return radius;}  // Driver codeint main(){       // Get the sides of the triangle    float l = 4, b = 3;       // Find the radius of the circumcircle    printf("%f\n",findRadiusOfcircumcircle(l, b));    return 0;}
// Java Program to find the radius// of the circumcircle of the given// rectangleimport java.util.*;import java.lang.*;import java.io.*; class GFG{ // Function to find the radius// of the circumcirclestatic float findRadiusOfcircumcircle(float l,                                      float b){     // the sides cannot be negative    if (l < 0 || b < 0)        return -1;     // Radius of the circumcircle    float radius = (float) Math.sqrt(Math.pow(l, 2) +                           Math.pow(b, 2)) / 2;     // Return the radius    return radius;} // Driver codepublic static void main(String args[]){     // Get the sides of the triangle    float l = 4, b = 3;    // Find the radius of the circumcircle    System.out.println(findRadiusOfcircumcircle(l, b));}} // This code is contributed by Subhadeep
# Python Program to find the# radius of the circumcircle# of the given rectangleimport math # Function to find the radius# of the circumcircledef findRadiusOfcircumcircle(l, b):     # the sides cannot be negative    if (l < 0 or b < 0):        return -1;     # Radius of the circumcircle    radius = (math.sqrt(pow(l, 2) +                        pow(b, 2)) / 2);     # Return the radius    return radius; # Driver code # Get the sides of the trianglel = 4;b = 3;     # Find the radius of the circumcircleprint(findRadiusOfcircumcircle(l, b)); # This code is contributed# by Shivi_Aggarwal
// C# Program to find the radius// of the circumcircle of the// given rectangleusing System; class GFG{ // Function to find the radius// of the circumcirclestatic float findRadiusOfcircumcircle(float l,                                       float b){     // the sides cannot be negative    if (l < 0 || b < 0)        return -1;     // Radius of the circumcircle    float radius = (float) Math.Sqrt(Math.Pow(l, 2) +                           Math.Pow(b, 2)) / 2;     // Return the radius    return radius;} // Driver codepublic static void Main(){     // Get the sides of the triangle    float l = 4, b = 3;         // Find the radius of the circumcircle    Console.WriteLine(findRadiusOfcircumcircle(l, b));}} // This code is contributed by anuj_67
<?php// PHP Program to find the radius// of the circumcircle of the// given rectangle // Function to find the radius// of the circumcirclefunction findRadiusOfcircumcircle($l, $b){     // the sides cannot be negative    if ($l < 0 || $b < 0)        return -1;     // Radius of the circumcircle    $radius = sqrt(pow($l, 2) +                   pow($b, 2)) / 2;     // Return the radius    return $radius;} // Driver code // Get the sides of the triangle$l = 4; $b = 3; // Find the radius of the circumcircleecho findRadiusOfcircumcircle($l, $b); // This code is contributed by anuj_67?>
<script> // javascript Program to find the radius// of the circumcircle of the given// rectangle // Function to find the radius// of the circumcirclefunction findRadiusOfcircumcircle(l,b){     // the sides cannot be negative    if (l < 0 || b < 0)        return -1;     // Radius of the circumcircle    var radius = Math.sqrt(Math.pow(l, 2) +                           Math.pow(b, 2)) / 2;     // Return the radius    return radius;} // Driver code  // Get the sides of the trianglevar l = 4, b = 3;// Find the radius of the circumcircledocument.write(findRadiusOfcircumcircle(l, b).toFixed(6)); // This code contributed by shikhasingrajput </script>
Output: 
2.5
 
tufan_gupta2000
vt_m
Shivi_Aggarwal
Akanksha_Rai
shikhasingrajput
sooda367
kothavvsaakash
circle
square-rectangle
Geometric
School Programming
Geometric
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Convex Hull using Divide and Conquer Algorithm
Equation of circle when three points on the circle are given
Circle and Lattice Points
Orientation of 3 ordered points
Program to find slope of a line
Python Dictionary
Arrays in C/C++
Inheritance in C++
Reverse a string in Java
Interfaces in Java