Give a rectangle with length l & breadth b, which inscribes a rhombus, which in turn inscribes a circle. The task is to find the radius of this circle.
Examples:
Input: l = 5, b = 3
Output: 1.28624
Input: l = 6, b = 4
Output: 1.6641

Approach: From the figure, it is clear that diagonals, x & y, are equal to the length and breadth of the rectangle.
Also radius of the circle, r, inside a rhombus is = xy/2?(x^2+y^2).
So, radius of the circle in terms of l & b is = lb/2?(l^2+b^2).
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
float circleradius( float l, float b)
{
if (l < 0 || b < 0)
return -1;
float r = (l * b) / (2 * sqrt (( pow (l, 2) + pow (b, 2))));
return r;
}
int main()
{
float l = 5, b = 3;
cout << circleradius(l, b) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
static float circleradius( float l, float b)
{
if (l < 0 || b < 0 )
return - 1 ;
float r = ( float )((l * b) / ( 2 * Math.sqrt((Math.pow(l, 2 ) + Math.pow(b, 2 )))));
return r;
}
public static void main (String[] args) {
float l = 5 , b = 3 ;
System.out.print (circleradius(l, b)) ;
}
}
|
Python3
from math import sqrt
def circleradius(l, b):
if (l < 0 or b < 0 ):
return - 1
r = (l * b) / ( 2 * sqrt(( pow (l, 2 ) +
pow (b, 2 ))));
return r
if __name__ = = '__main__' :
l = 5
b = 3
print ( "{0:.5}" . format (circleradius(l, b)))
|
C#
using System;
class GFG
{
static float circleradius( float l,
float b)
{
if (l < 0 || b < 0)
return -1;
float r = ( float )((l * b) /
(2 * Math.Sqrt((Math.Pow(l, 2) +
Math.Pow(b, 2)))));
return r;
}
public static void Main ()
{
float l = 5, b = 3;
Console.WriteLine(circleradius(l, b));
}
}
|
PHP
<?php
function circleradius( $l , $b )
{
if ( $l < 0 || $b < 0)
return -1;
$r = ( $l * $b ) / (2 * sqrt((pow( $l , 2) +
pow( $b , 2))));
return $r ;
}
$l = 5;
$b = 3;
echo circleradius( $l , $b ), "\n" ;
?>
|
Javascript
<script>
function circleradius(l , b)
{
if (l < 0 || b < 0)
return -1;
var r = ((l * b) / (2 * Math.sqrt((Math.pow(l, 2) + Math.pow(b, 2)))));
return r;
}
var l = 5, b = 3;
document.write(circleradius(l, b).toFixed(5)) ;
</script>
|
Time complexity: O(logn) as it is using inbuilt sqrt function
Auxiliary Space: O(1) since using constant variables
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
27 Aug, 2022
Like Article
Save Article