Given here is a cube of side length **a**, the task is to find the biggest sphere that can be inscribed within it.

**Examples:**

Input:a = 4Output:2Input:a = 5Output:2.5

**Approach**:

From the 2d diagram it is clear that,

2r = a,

where,a= side of the cube

r= radius of the sphere

sor = a/2.

Below is the implementation of the above approach:

## C++

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

*chevron_right*

*filter_none*

## Java

`// Java Program to find the biggest sphere ` `// inscribed within a cube ` ` ` `class` `GFG{ ` `// Function to find the radius of the sphere ` `static` `float` `sphere(` `float` `a) ` `{ ` ` ` ` ` `// side cannot be negative ` ` ` `if` `(a < ` `0` `) ` ` ` `return` `-` `1` `; ` ` ` ` ` `// radius of the sphere ` ` ` `float` `r = a / ` `2` `; ` ` ` ` ` `return` `r; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `float` `a = ` `5` `; ` ` ` `System.out.println(sphere(a)); ` ` ` `} ` `} ` `// This code is contributed by mits ` |

*chevron_right*

*filter_none*

## Python3

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

*chevron_right*

*filter_none*

## C#

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

*chevron_right*

*filter_none*

## PHP

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

*chevron_right*

*filter_none*

**Output:**

2.5

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 cube that can be inscribed within the sphere
- Largest sphere that can be inscribed in a right circular cylinder inscribed in a frustum
- 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
- 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
- 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
- Check whether a point lies inside a sphere or not
- Percentage increase in volume of the cube if a side of cube is increased by a given percentage
- Count cubes of size K inscribed in a cube of size N
- Area of a triangle inscribed in a rectangle which is inscribed in an ellipse
- Area of a square inscribed in a circle which is inscribed in an equilateral triangle
- Area of a circle inscribed in a rectangle which is inscribed in a semicircle
- Area of a square inscribed in a circle which is inscribed in a hexagon
- Radius of the biggest possible circle inscribed in rhombus which in turn is inscribed in a rectangle
- Biggest Reuleaux Triangle inscribed within a Square inscribed in an equilateral triangle

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.