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 = 8Output: 4Input: r = 5, h= 10Output: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++

`// C++ Program to find the biggest sphere ` `// that can be fit within a right circular cylinder ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the biggest sphere ` `float` `sph(` `float` `r, ` `float` `h) ` `{ ` ` ` ` ` `// radius and height cannot be negative ` ` ` `if` `(r < 0 && h < 0) ` ` ` `return` `-1; ` ` ` ` ` `// radius of sphere ` ` ` `float` `R = r; ` ` ` `return` `R; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `float` `r = 4, h = 8; ` ` ` `cout << sph(r, h) << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java Program to find the biggest ` `// sphere that can be fit within a ` `// right circular cylinder ` `import` `java.io.*; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to find the biggest sphere ` `static` `float` `sph(` `float` `r, ` `float` `h) ` `{ ` ` ` ` ` `// radius and height cannot ` ` ` `// be negative ` ` ` `if` `(r < ` `0` `&& h < ` `0` `) ` ` ` `return` `-` `1` `; ` ` ` ` ` `// radius of sphere ` ` ` `float` `R = r; ` ` ` `return` `R; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main (String[] args) ` `{ ` ` ` `float` `r = ` `4` `, h = ` `8` `; ` ` ` `System.out.println(sph(r, h)); ` `} ` `} ` ` ` `// This code is contributed ` `// by inder_verma ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 Program to find the biggest ` `# sphere that can be fit within a right ` `# circular cylinder ` ` ` `# Function to find the biggest sphere ` `def` `sph(r, h): ` ` ` ` ` `# radius and height cannot ` ` ` `# be negative ` ` ` `if` `(r < ` `0` `and` `h < ` `0` `): ` ` ` `return` `-` `1` ` ` ` ` `# radius of sphere ` ` ` `R ` `=` `r ` ` ` `return` `float` `(R) ` ` ` `# Driver code ` `r, h ` `=` `4` `, ` `8` `print` `(sph(r, h)) ` ` ` `# This code is contributed ` `# by PrinciRaj1992 ` |

*chevron_right*

*filter_none*

## C#

`// C# Program to find the biggest ` `// sphere that can be fit within a ` `// right circular cylinder ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to find the biggest sphere ` `static` `float` `sph(` `float` `r, ` `float` `h) ` `{ ` ` ` ` ` `// radius and height cannot ` ` ` `// be negative ` ` ` `if` `(r < 0 && h < 0) ` ` ` `return` `-1; ` ` ` ` ` `// radius of sphere ` ` ` `float` `R = r; ` ` ` `return` `R; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main () ` `{ ` ` ` `float` `r = 4, h = 8; ` ` ` `Console.WriteLine(sph(r, h)); ` `} ` `} ` ` ` `// This code is contributed ` `// by shs.. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` ` ` `// PHP Program to find the biggest sphere ` `// that can be fit within a right circular cylinder ` ` ` `// Function to find the biggest sphere ` `function` `sph(` `$r` `, ` `$h` `) ` `{ ` ` ` ` ` `// radius and height cannot be negative ` ` ` `if` `(` `$r` `< 0 && ` `$h` `< 0) ` ` ` `return` `-1; ` ` ` ` ` `// radius of sphere ` ` ` `$R` `= ` `$r` `; ` ` ` `return` `$R` `; ` `} ` ` ` `// Driver code ` ` ` ` ` `$r` `= 4 ;` `$h` `= 8; ` ` ` `echo` `sph(` `$r` `, ` `$h` `); ` ` ` `// This code is contributed ` `// by shs.. ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

4

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Volume of largest right circular cylinder within a Sphere
- Largest right circular cone that can be inscribed within a sphere which is inscribed within a cube
- Largest sphere that can be inscribed within a cube which is in turn inscribed within a right circular cone
- Largest right circular cylinder that can be inscribed within a cone which is in turn inscribed within a cube
- Largest sphere that can be inscribed in a right circular cylinder inscribed in a frustum
- Largest right circular cone that can be inscribed within a sphere
- Biggest Reuleaux Triangle within a Square which is inscribed within a Right angle Triangle
- Largest cube that can be inscribed within a right circular cylinder
- Longest rod that can be inserted within a right circular cylinder
- Largest right circular cylinder that can be inscribed within a cone
- Largest right circular cylinder within a frustum
- Largest right circular cylinder within a cube
- Biggest Reuleaux Triangle within a Square which is inscribed within a Circle
- Biggest Reuleaux Triangle inscribed within a square which is inscribed within an ellipse
- Biggest Reuleaux Triangle inscribed within a square which is inscribed within a hexagon
- Calculate Volume and Surface area Of Sphere
- Percentage increase in volume of the sphere if radius is increased by a given percentage
- Minimum volume of cone that can be circumscribed about a sphere of radius R
- Calculate Volume, Curved Surface Area and Total Surface Area Of Cylinder
- Largest cube that can be inscribed within the sphere

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.