Given here is a right circular cylinder of height h and radius r. The task is to find the volume of biggest cube that can be inscribed within it.
Examples:
Input: h = 3, r = 2
Output: volume = 27
Input: h = 5, r = 4
Output: volume = 125

Approach: From the figure, it can be clearly understand that side of the cube = height of the cylinder.
So, the volume = (height)^3
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
float cube( float h, float r)
{
if (h < 0 && r < 0)
return -1;
float a = pow (h, 3);
return a;
}
int main()
{
float h = 5, r = 4;
cout << cube(h, r) << endl;
return 0;
}
|
Java
class Solution
{
static float cube( float h, float r)
{
if (h < 0 && r < 0 )
return - 1 ;
float a = ( float )Math.pow(h, 3 );
return a;
}
public static void main(String args[])
{
float h = 5 , r = 4 ;
System.out.println( cube(h, r) );
}
}
|
Python 3
import math
def cube(h, r):
if (h < 0 and r < 0 ):
return - 1
a = math. pow (h, 3 )
return a
h = 5 ; r = 4 ;
print (cube(h, r));
|
C#
using System;
class GFG
{
static float cube( float h, float r)
{
if (h < 0 && r < 0)
return -1;
float a = ( float )Math.Pow(h, 3);
return a;
}
public static void Main()
{
float h = 5, r = 4;
Console.Write( cube(h, r) );
}
}
|
PHP
<?php
function cube( $h , $r )
{
if ( $h < 0 && $r < 0)
return -1;
$a = pow( $h , 3);
return $a ;
}
$h = 5;
$r = 4;
echo cube( $h , $r );
?>
|
Javascript
<script>
function cube(h , r)
{
if (h < 0 && r < 0)
return -1;
var a = Math.pow(h, 3);
return a;
}
var h = 5, r = 4;
document.write( cube(h, r) );
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1), As we are not using any extra space.