Largest cone that can be inscribed within a cube
Last Updated :
27 Aug, 2022
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,
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
float coneRadius( float a)
{
if (a < 0)
return -1;
float r = a / sqrt (2);
return r;
}
float coneHeight( float a)
{
if (a < 0)
return -1;
float h = a;
return h;
}
int main()
{
float a = 6;
cout << "r = " << coneRadius(a) << ", "
<< "h = " << coneHeight(a) << endl;
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
class GFG
{
static float coneRadius( float a)
{
if (a < 0 )
return - 1 ;
float r = ( float )(a / Math.sqrt( 2 ));
return r;
}
static float coneHeight( float a)
{
if (a < 0 )
return - 1 ;
float h = a;
return h;
}
public static void main(String args[])
{
float a = 6 ;
System.out.println( "r = " + coneRadius(a) +
", " + "h = " + coneHeight(a));
}
}
|
Python 3
import math
def coneRadius(a):
if (a < 0 ):
return - 1
r = a / math.sqrt( 2 )
return r
def coneHeight(a):
if (a < 0 ):
return - 1
h = a
return h
if __name__ = = "__main__" :
a = 6
print ( "r = " , coneRadius(a) ,
"h = " , coneHeight(a))
|
C#
using System;
class GFG
{
static float coneRadius( float a)
{
if (a < 0)
return -1;
float r = ( float )(a / Math.Sqrt(2));
return r;
}
static float coneHeight( float a)
{
if (a < 0)
return -1;
float h = a;
return h;
}
public static void Main()
{
float a = 6;
Console.WriteLine( "r = " + coneRadius(a) +
", " + "h = " + coneHeight(a));
}
}
|
PHP
<?php
function coneRadius( $a )
{
if ( $a < 0)
return -1;
$r = $a / sqrt(2);
return $r ;
}
function coneHeight( $a )
{
if ( $a < 0)
return -1;
$h = $a ;
return $h ;
}
$a = 6;
echo ( "r = " );
echo coneRadius( $a );
echo ( ", " );
echo ( "h = " );
echo (coneHeight( $a ));
?>
|
Javascript
<script>
function coneRadius(a)
{
if (a < 0)
return -1;
var r = (a / Math.sqrt(2));
return r;
}
function coneHeight(a)
{
if (a < 0)
return -1;
var h = a;
return h;
}
var a = 6;
document.write( "r = " + coneRadius(a).toFixed(5) +
", " + "h = " + coneHeight(a));
</script>
|
Output:
r = 4.24264, h = 6
Time Complexity: O(1)
Auxiliary Space: O(1), since no extra space has been taken.
Share your thoughts in the comments
Please Login to comment...