# Largest sphere that can be inscribed within a cube which is in turn inscribed within a right circular cone

Given here is a right circular cone of radius **r** and perpendicular height **h**, which is inscribed in a cube which in turn is inscribed in a sphere, the task is to find the radius of the sphere.

**Examples:**

Input:h = 5, r = 6Output:1.57306Input:h = 8, r = 11Output:2.64156

**Approach**:

- Let the side of the cube =
**a** - Let the radius of the sphere =
**R** - We know,
**a=h*r√2/(h+√2*r)**(Please refer here) - Also,
**R=a/2**(Please refer here) - So,
**R = (h*r√2/(h+√2*r))/2**

Below is the implementation of the above approach:

## C++

`// C++ Program to find the biggest sphere ` `// which is inscribed within a cube which in turn ` `// inscribed within a right circular cone ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the radius of the sphere ` `float` `sphereSide(` `float` `h, ` `float` `r) ` `{ ` ` ` `// height and radius cannot be negative ` ` ` `if` `(h < 0 && r < 0) ` ` ` `return` `-1; ` ` ` ` ` `// radius of the sphere ` ` ` `float` `R = ((h * r * ` `sqrt` `(2)) / (h + ` `sqrt` `(2) * r)) / 2; ` ` ` ` ` `return` `R; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `float` `h = 5, r = 6; ` ` ` ` ` `cout << sphereSide(h, r) << endl; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java Program to find the biggest sphere ` `// which is inscribed within a cube which in turn ` `// inscribed within a right circular cone ` `import` `java.lang.Math; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to find the radius of the sphere ` `static` `float` `sphereSide(` `float` `h, ` `float` `r) ` `{ ` ` ` `// height and radius cannot be negative ` ` ` `if` `(h < ` `0` `&& r < ` `0` `) ` ` ` `return` `-` `1` `; ` ` ` ` ` `// radius of the sphere ` ` ` `float` `R = (` `float` `)((h * r * Math.sqrt(` `2` `)) / ` ` ` `(h + Math.sqrt(` `2` `) * r)) / ` `2` `; ` ` ` ` ` `return` `R; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `float` `h = ` `5` `, r = ` `6` `; ` ` ` ` ` `System.out.println(sphereSide(h, r)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Code_Mech. ` |

*chevron_right*

*filter_none*

## Python3

`# Program to find the biggest sphere ` `# which is inscribed within a cube which in turn ` `# inscribed within a right circular cone ` `import` `math ` ` ` `# Function to find the radius of the sphere ` `def` `sphereSide(h, r): ` ` ` ` ` `# height and radius cannot be negative ` ` ` `if` `h < ` `0` `and` `r < ` `0` `: ` ` ` `return` `-` `1` ` ` ` ` `# radius of the sphere ` ` ` `R ` `=` `(((h ` `*` `r ` `*` `math.sqrt(` `2` `))) ` `/` ` ` `(h ` `+` `math.sqrt(` `2` `) ` `*` `r) ` `/` `2` `) ` ` ` ` ` `return` `R ` ` ` `# Driver code ` `h ` `=` `5` `; r ` `=` `6` `print` `(sphereSide(h, r)) ` ` ` `# This code is contributed by Shrikant13 ` |

*chevron_right*

*filter_none*

## C#

// C# Program to find the biggest sphere

// which is inscribed within a cube which in turn

// inscribed within a right circular cone

using System;

class GFG

{

// Function to find the radius of the sphere

static float sphereSide(float h, float r)

{

// height and radius cannot be negative

if (h < 0 && r < 0)
return -1;
// radius of the sphere
float R = (float)((h * r * Math.Sqrt(2)) /
(h + Math.Sqrt(2) * r)) / 2;
return R;
}
// Driver code
public static void Main()
{
float h = 5, r = 6;
Console.WriteLine(sphereSide(h, r));
}
}
// This code is contributed by Code_Mech
[tabby title="PHP"]

`<?php ` `// PHP Program to find the biggest sphere ` `// which is inscribed within a cube which in turn ` `// inscribed within a right circular cone ` ` ` `// Function to find the radius of the sphere ` `function` `sphereSide(` `$h` `, ` `$r` `) ` `{ ` ` ` `// height and radius cannot be negative ` ` ` `if` `(` `$h` `< 0 && ` `$r` `< 0) ` ` ` `return` `-1; ` ` ` ` ` `// radius of the sphere ` ` ` `$R` `= ((` `$h` `* ` `$r` `* sqrt(2)) / ` ` ` `(` `$h` `+ sqrt(2) * ` `$r` `)) / 2; ` ` ` ` ` `return` `$R` `; ` `} ` ` ` `// Driver code ` `$h` `= 5; ` `$r` `= 6; ` ` ` `echo` `(sphereSide(` `$h` `, ` `$r` `)); ` ` ` `// This code is contributed by Code_Mech. ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

1.57306

## Recommended Posts:

- Largest right circular cylinder that can be inscribed within a cone which is in turn inscribed within a cube
- Largest right circular cone that can be inscribed within a sphere which is inscribed within a cube
- Largest right circular cone that can be inscribed within a sphere
- Largest cube that can be inscribed within a right circular cone
- Largest sphere that can be inscribed in a right circular cylinder inscribed in a frustum
- Largest cone that can be inscribed within a cube
- Largest cube that can be inscribed within the sphere
- Largest sphere that can be inscribed inside a cube
- Largest right circular cylinder that can be inscribed within a cone
- Largest ellipse that can be inscribed within a rectangle which in turn is inscribed within a semicircle
- Largest cube that can be inscribed within a right circular cylinder
- Radius of the biggest possible circle inscribed in rhombus which in turn is inscribed in a rectangle
- Largest square that can be inscribed within a hexagon which is inscribed within an equilateral triangle
- Biggest Reuleaux Triangle inscribed within a square which is inscribed within a hexagon
- Biggest Reuleaux Triangle inscribed within a square which is inscribed within an ellipse

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.