Given a right circular cylinder of radius
and height
. The task is to find the radius of the biggest sphere that can be inscribed within it.
Examples:
Input : r = 4, h = 8
Output : 4
Input : r = 5, h= 10
Output :5

Approach: From the diagram, it is clear that the radius of the sphere will be clearly equal to the base radius of cylinder.
So, R = r
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
float sph( float r, float h)
{
if (r < 0 && h < 0)
return -1;
float R = r;
return R;
}
int main()
{
float r = 4, h = 8;
cout << sph(r, h) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG
{
static float sph( float r, float h)
{
if (r < 0 && h < 0 )
return - 1 ;
float R = r;
return R;
}
public static void main (String[] args)
{
float r = 4 , h = 8 ;
System.out.println(sph(r, h));
}
}
|
Python3
def sph(r, h):
if (r < 0 and h < 0 ):
return - 1
R = r
return float (R)
r, h = 4 , 8
print (sph(r, h))
|
C#
using System;
class GFG
{
static float sph( float r, float h)
{
if (r < 0 && h < 0)
return -1;
float R = r;
return R;
}
public static void Main ()
{
float r = 4, h = 8;
Console.WriteLine(sph(r, h));
}
}
|
PHP
<?php
function sph( $r , $h )
{
if ( $r < 0 && $h < 0)
return -1;
$R = $r ;
return $R ;
}
$r = 4 ; $h = 8;
echo sph( $r , $h );
?>
|
Javascript
<script>
function sph(r , h)
{
if (r < 0 && h < 0)
return -1;
var R = r;
return R;
}
var r = 4, h = 8;
document.write(sph(r, h));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
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 :
11 Jul, 2022
Like Article
Save Article