Given here is a cube of side length a. We have to find the height and the radius of the biggest right circular cone that can be inscribed within it.
Examples:
Input : a = 6 Output : r = 4.24264, h = 6 Input : a = 10 Output : r = 7.07107, h = 10

Approach:
Let height of the cone = h.
and, radius of the cone = r.
From the diagram, we can clearly understand that,
- r = a/√2
- h = a
Below is the implementation of the above approach:
C++
// C++ Program to find the biggest cone// inscribed within a cube#include <bits/stdc++.h>using namespace std;// Function to find the radius of the conefloat coneRadius(float a){ // side cannot be negative if (a < 0) return -1; // radius of the cone float r = a / sqrt(2); return r;}// Function to find the height of the conefloat coneHeight(float a){ // side cannot be negative if (a < 0) return -1; // height of the cone float h = a; return h;}// Driver codeint main(){ float a = 6; cout << "r = " << coneRadius(a) << ", " << "h = " << coneHeight(a) << endl; return 0;} |
Java
// Java Program to find the biggest// cone inscribed within a cubeimport java.util.*;import java.lang.*;class GFG{// Function to find the radius// of the conestatic float coneRadius(float a){ // side cannot be negative if (a < 0) return -1; // radius of the cone float r = (float)(a / Math.sqrt(2)); return r;}// Function to find the height// of the conestatic float coneHeight(float a){ // side cannot be negative if (a < 0) return -1; // height of the cone float h = a; return h;}// Driver codepublic static void main(String args[]){ float a = 6; System.out.println("r = " + coneRadius(a) + ", " + "h = " + coneHeight(a));}}// This code is contributed// by Akanksha Rai |
Python 3
# Python 3 Program to find the biggest# cone inscribed within a cubeimport math# Function to find the radius# of the conedef coneRadius(a): # side cannot be negative if (a < 0): return -1 # radius of the cone r = a / math.sqrt(2) return r# Function to find the height of the conedef coneHeight(a): # side cannot be negative if (a < 0): return -1 # height of the cone h = a return h# Driver codeif __name__ == "__main__": a = 6 print("r = ", coneRadius(a) , "h = ", coneHeight(a))# This code is contributed by ChitraNayal |
C#
// C# Program to find the biggest// cone inscribed within a cubeusing System;class GFG{// Function to find the radius// of the conestatic float coneRadius(float a){ // side cannot be negative if (a < 0) return -1; // radius of the cone float r = (float)(a / Math.Sqrt(2)); return r;}// Function to find the height// of the conestatic float coneHeight(float a){ // side cannot be negative if (a < 0) return -1; // height of the cone float h = a; return h;}// Driver codepublic static void Main(){ float a = 6; Console.WriteLine("r = " + coneRadius(a) + ", " + "h = " + coneHeight(a));}}// This code is contributed// by Akanksha Rai |
PHP
<?php// PHP Program to find the biggest// cone inscribed within a cube// Function to find the radius// of the conefunction coneRadius($a){ // side cannot be negative if ($a < 0) return -1; // radius of the cone $r = $a / sqrt(2); return $r;}// Function to find the height// of the conefunction coneHeight($a){ // side cannot be negative if ($a < 0) return -1; // height of the cone $h = $a; return $h;}// Driver code$a = 6;echo ("r = ");echo coneRadius($a);echo (", ");echo ("h = ");echo (coneHeight($a));// This code is contributed// by Shivi_Aggarwal?> |
Javascript
<script>// javascript Program to find the biggest// cone inscribed within a cube// Function to find the radius// of the conefunction coneRadius(a){ // side cannot be negative if (a < 0) return -1; // radius of the cone var r = (a / Math.sqrt(2)); return r;}// Function to find the height// of the conefunction coneHeight(a){ // side cannot be negative if (a < 0) return -1; // height of the cone var h = a; return h;}// Driver codevar a = 6;document.write("r = " + coneRadius(a).toFixed(5) + ", " + "h = " + coneHeight(a));// This code is contributed by 29AjayKumar</script> |
r = 4.24264, h = 6
Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.


