Largest sphere that can be inscribed inside a cube

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 cuber= 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;` `}` |

## 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` |

## 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` |

## 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` |

## 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` `?>` |

## Javascript

`<script>` `// javascript 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` ` ` `var` `r = a / 2;` ` ` `return` `r;` `}` `// Driver code` `var` `a = 5;` `document.write(sphere(a));` `// This code is contributed by 29AjayKumar` `</script>` |

**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.