Largest right circular cylinder that can be inscribed within a cone which is in turn inscribed within a cube
Last Updated :
20 Aug, 2022
Given here is a cube of side length a, which inscribes a cone which in turn inscribes a right circular cylinder. The task is to find the largest possible volume of this cylinder.
Examples:
Input: a = 5
Output: 232.593
Input: a = 8
Output: 952.699
Approach:
From the figure, it is very clear, height of cone, H = a and radius of the cone, R = a?2, please refer Largest cone that can be inscribed within a cube.
and, radius of the cylinder, r = 2R/3 and height of the cylinder, h = 2H/3, please refer Largest right circular cylinder that can be inscribed within a cone.
So, radius of cylinder with respect to cube, r = 2a?2/3 and height of cylinder with respect to cube, h = 2a/3.
So, volume of the cylinder, V = 16?a^3/27.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
float cyl( float a)
{
if (a < 0)
return -1;
float r = (2 * a * sqrt (2)) / 3;
float h = (2 * a) / 3;
float V = 3.14 * pow (r, 2) * h;
return V;
}
int main()
{
float a = 5;
cout << cyl(a) << endl;
return 0;
}
|
Java
import java.lang.Math;
class cfg
{
static float cyl( float a)
{
if (a < 0 )
return - 1 ;
float r = ( 2 * a *( float )(Math.sqrt ( 2 )) / 3 );
float h = ( 2 * a) / 3 ;
float V =( 3 .14f *( float )(Math.pow(r, 2 ) * h));
return V;
}
public static void main(String[] args)
{
float a = 5 ;
System.out.println(cyl(a));
}
}
|
Python3
import math as mt
def cyl(a):
if (a < 0 ):
return - 1
r = ( 2 * a * mt.sqrt( 2 )) / 3
h = ( 2 * a) / 3
V = 3.14 * pow (r, 2 ) * h
return V
a = 5
print (cyl(a))
|
C#
using System;
class GFG
{
static float cyl( float a)
{
if (a < 0)
return -1;
float r = (2 * a * ( float )(Math.Sqrt (2)) / 3);
float h = (2 * a) / 3;
float V =(3.14f * ( float )(Math.Pow(r, 2) * h));
return V;
}
public static void Main()
{
float a = 5;
Console.Write(cyl(a));
}
}
|
PHP
<?php
function cyl( $a )
{
if ( $a < 0)
return -1;
$r = (2 * $a * sqrt(2)) / 3;
$h = (2 * $a ) / 3;
$V = 3.14 * pow( $r , 2) * $h ;
return $V ;
}
$a = 5;
echo cyl( $a );
?>
|
Javascript
<script>
function cyl(a)
{
if (a < 0)
return -1;
var r = (2 * a *(Math.sqrt (2)) / 3);
var h = (2 * a) / 3;
var V =(3.14 *(Math.pow(r, 2) * h));
return V;
}
var a = 5;
document.write(cyl(a).toFixed(5));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...