Largest right circular cone that can be inscribed within a sphere - GeeksforGeeks

17 Mar, 2021
Given sphere of radius . The task is to find the radius of base and height of the largest right circular cone that can be inscribed within it.Examples:  
Input : R = 10
Output : r = 9.42809, h = 13.3333

Input : R = 25
Output : r = 23.5702, h = 33.3333
 
 
Approach:Let the radius of the cone = r height of the cone = hFrom the diagram it is clear that: x = √(R^2 – r^2) and h=x+R Now using these values we get,  
To maximize the volume of the cone(V): V = (πr2h)/3From the diagram, V = (πr2R)/3 + πr2√(R2 – r2)/3Taking first derivative of V with respect to r we get, Now, setting dV/dr = 0 we get, Squaring both sides and solving we get, since, h = R + √(R2 – r2)Now calculating the second derivative we get 
*** QuickLaTeX cannot compile formula:
 

*** Error message:
Error: Nothing to show, formula is empty

Thus r=(2R√2)/3 is point of maxima So, h = 4R/3  
Below is the implementation of the above approach:  
C++
Java
Python3
C#
PHP
Javascript
// C++ Program to find the biggest cone// that can be inscribed within a sphere#include <bits/stdc++.h>using namespace std; // Function to find the radius of the conefloat coner(float R){     // radius cannot be negative    if (R < 0)        return -1;     // radius of the cone    float r = (2 * sqrt(2) * R) / 3;    return r;} // Function to find the height of the conefloat coneh(float R){     // side cannot be negative    if (R < 0)        return -1;     // height of the cone    float h = (4 * R) / 3;    return h;} // Driver codeint main(){    float R = 10;     cout << "r = " << coner(R) << ", "         << "h = " << coneh(R) << endl;     return 0;}
// Java Program to find the biggest cone// that can be inscribed within a sphereimport java.util.*;import java.lang.*; class GFG{// Function to find the radius// of the conestatic float coner(float R){    // radius cannot be negative    if (R < 0)        return -1;     // radius of the cone    float r = (float)(2 *            Math.sqrt(2) * R) / 3;    return r;} // Function to find the// height of the conestatic float coneh(float R){     // side cannot be negative    if (R < 0)        return -1;     // height of the cone    float h = (4 * R) / 3;    return h;} // Driver codepublic static void main(String args[]){    float R = 10;     System.out.println("r = " + coner(R) +                       ", " + "h = " + coneh(R));}} // This code is contributed// by Akanksha Rai
# Python 3 Program to find the biggest cone# that can be inscribed within a sphereimport math # Function to find the radius# of the conedef coner(R):         # radius cannot be negative    if (R < 0):        return -1;         # radius of the cone    r = (2 * math.sqrt(2) * R) / 3    return float(r) # Function to find the height# of the conedef coneh(R):         # side cannot be negative    if (R < 0):        return -1;     # height of the cone    h = (4 * R) / 3    return float(h) # Driver codeR = 10print("r = " , coner(R) ,      ", ", "h = " , coneh(R)) # This code is contributed# by 29AjayKumar
// C# Program to find the biggest cone// that can be inscribed within a sphereusing System; class GFG{// Function to find the radius// of the conestatic float coner(float R){    // radius cannot be negative    if (R < 0)        return -1;     // radius of the cone    float r = (float)(2 *               Math.Sqrt(2) * R) / 3;    return r;} // Function to find the// height of the conestatic float coneh(float R){     // side cannot be negative    if (R < 0)        return -1;     // height of the cone    float h = (4 * R) / 3;    return h;} // Driver codepublic static void Main(){    float R = 10;     Console.WriteLine("r = " + coner(R) +                      ", " + "h = " + coneh(R));}} // This code is contributed// by Akanksha Rai
<?php// PHP Program to find the biggest// cone that can be inscribed// within a sphere // Function to find the radius// of the conefunction coner($R){     // radius cannot be negative    if ($R < 0)        return -1;     // radius of the cone    $r = (2 * sqrt(2) * $R) / 3;    return $r;} // Function to find the height// of the conefunction coneh($R){     // side cannot be negative    if ($R < 0)        return -1;     // height of the cone    $h = (4 * $R) / 3;    return $h;} // Driver code$R = 10; echo ("r = ");echo coner($R);echo (", ");echo ("h = ");echo (coneh($R)); // This code is contributed// by Shivi_Aggarwal?>
<script>// javascript Program to find the biggest cone// that can be inscribed within a sphere // Function to find the radius// of the conefunction coner(R){     // radius cannot be negative    if (R < 0)        return -1;     // radius of the cone    var r = (2 *            Math.sqrt(2) * R) / 3;    return r;} // Function to find the// height of the conefunction coneh(R){     // side cannot be negative    if (R < 0)        return -1;     // height of the cone    var h = (4 * R) / 3;    return h;} // Driver codevar R = 10;document.write("r = " + coner(R).toFixed(5) +                   ", " + "h = " + coneh(R).toFixed(5)); // This code is contributed by 29AjayKumar</script>
r = 9.42809, h = 13.3333
 
Shivi_Aggarwal
Akanksha_Rai
29AjayKumar
mohitg593
circle
Geometric
Mathematical
Mathematical
Geometric
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Haversine formula to find distance between two points on a sphere
Program to find slope of a line
Equation of circle when three points on the circle are given
Program to find line passing through 2 Points
Maximum Manhattan distance between a distinct pair from N coordinates
Program for Fibonacci numbers
Write a program to print all permutations of a given string
C++ Data Types
Set in C++ Standard Template Library (STL)
Coin Change | DP-7