Given here is a sphere of radius **r**, the task is to find the side of the largest cube that can fit inside in it.

**Examples:**

Input:r = 8Output:9.2376Input:r = 5Output:5.7735

**Approach**:

Side of the cube =

a

Radius of the sphere =r

From the diagonal, it is clear that, diagonal of the cube = diameter of the sphere,

a√3 = 2ror,a = 2r/√3

Below is the implementation:

## C++

`// C++ Program to find the biggest cube ` `// inscribed within a sphere ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the side of the cube ` `float` `largestCube(` `float` `r) ` `{ ` ` ` ` ` `// radius cannot be negative ` ` ` `if` `(r < 0) ` ` ` `return` `-1; ` ` ` ` ` `// side of the cube ` ` ` `float` `a = (2 * r) / ` `sqrt` `(3); ` ` ` `return` `a; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `float` `r = 5; ` ` ` `cout << largestCube(r) << endl; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java Program to find the biggest cube ` `// inscribed within a sphere ` `import` `java.util.*; ` `class` `Solution{ ` `// Function to find the side of the cube ` `static` `float` `largestCube(` `float` `r) ` `{ ` ` ` ` ` `// radius cannot be negative ` ` ` `if` `(r < ` `0` `) ` ` ` `return` `-` `1` `; ` ` ` ` ` `// side of the cube ` ` ` `float` `a = (` `2` `* r) / (` `float` `)Math.sqrt(` `3` `); ` ` ` `return` `a; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String args[]) ` `{ ` ` ` `float` `r = ` `5` `; ` ` ` `System.out.println( largestCube(r)); ` ` ` `} ` ` ` `} ` `//contributed by Arnab Kundu ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 Program to find the biggest ` `# cube inscribed within a sphere ` `from` `math ` `import` `sqrt ` ` ` `# Function to find the side of the cube ` `def` `largestCube(r): ` ` ` ` ` `# radius cannot be negative ` ` ` `if` `(r < ` `0` `): ` ` ` `return` `-` `1` ` ` ` ` `# side of the cube ` ` ` `a ` `=` `(` `2` `*` `r) ` `/` `sqrt(` `3` `) ` ` ` `return` `a ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `r ` `=` `5` ` ` `print` `(` `"{0:.6}"` `.` `format` `(largestCube(r))) ` ` ` `# This code is contributed ` `# by SURENDRA_GANGWAR ` |

*chevron_right*

*filter_none*

## C#

`// C# Program to find the biggest cube ` `// inscribed within a sphere ` `using` `System; ` `class` `Solution{ ` `// Function to find the side of the cube ` `static` `float` `largestCube(` `float` `r) ` `{ ` ` ` ` ` `// radius cannot be negative ` ` ` `if` `(r < 0) ` ` ` `return` `-1; ` ` ` ` ` `// side of the cube ` ` ` `float` `a = (2 * r) / (` `float` `)Math.Sqrt(3); ` ` ` `return` `a; ` `} ` ` ` `// Driver code ` `static` `void` `Main() ` `{ ` ` ` `float` `r = 5; ` ` ` `Console.WriteLine( largestCube(r)); ` ` ` `} ` ` ` `} ` `//This code is contributed by mits ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP Program to find the biggest ` `// cube inscribed within a sphere ` ` ` `// Function to find the side ` `// of the cube ` `function` `largestCube(` `$r` `) ` `{ ` ` ` ` ` `// radius cannot be negative ` ` ` `if` `(` `$r` `< 0) ` ` ` `return` `-1; ` ` ` ` ` `// side of the cube ` ` ` `$a` `= (float)((2 * ` `$r` `) / sqrt(3)); ` ` ` `return` `$a` `; ` `} ` ` ` `// Driver code ` `$r` `= 5; ` `echo` `largestCube(` `$r` `); ` ` ` `// This code is contributed by akt_mit ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

5.7735

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:

- 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 square that can be inscribed within a hexagon which is inscribed within an equilateral triangle
- Largest ellipse that can be inscribed within a rectangle which in turn is inscribed within a semicircle
- Largest sphere that can be inscribed inside 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
- Largest cone that can be inscribed within a cube
- Largest cube that can be inscribed within a right circular cone
- Largest cube that can be inscribed within a right circular cylinder
- 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
- Volume of largest right circular cylinder within a Sphere
- Biggest Reuleaux Triangle inscribed within a Square inscribed in an equilateral triangle
- Biggest Reuleaux Triangle within a Square which is inscribed within a Circle
- Biggest Reuleaux Triangle within a Square which is inscribed within a Right angle Triangle
- Area of largest triangle that can be inscribed within a rectangle
- Largest right circular cylinder that can be inscribed within a cone
- Largest Square that can be inscribed within a hexagon

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.