# Largest right circular cylinder that can be inscribed within a cone

Given a right circular cylinder which is inscribed in a cone of height **h** and base radius **r**. The task is to find the largest possible volume of the cylinder.

**Examples:**

Input:r = 4, h = 8Output:119.087Input:r = 5, h = 9Output:209.333

**Approach**: The volume of a cylinder is **V = πr^2h**

In this problem, first derive an equation for volume using similar triangles in terms of the height and radius of the cone. Once we have the modified the volume equation, we’ll take the derivative of the volume and solve for the largest value.

Let **x** be the radius of the cylinder and **y** be the distance from the top of the cone to the top of the inscribed cylinder. Therefore, the height of the cylinder is **h – y**

The volume of the inscribed cylinder is **V = πx^2(h-y)**.

We use the method of similar ratios to find a relationship between the height and radius, **h-y** and **x**.

**y/x = h/r
y = hx/r**

Substitute the equation for

**y**into the equation for volume, V.

V = πx^2(h-y)

V = πx^2(h-hx/r)

V = πx^2h – πx^3h/r

now,dV/dx = d(πx^2h – πx^3h/r)/dx

and settingdV/dx = 0

we get,x = 0, 2r/3

So,x = 2r/3

and,y = 2h/3

So,V = π8r^2h/27

Below is the implementation of the above approach:

## C++

`// C++ Program to find the biggest ` `// right circular cylinder that can ` `// be fit within a right circular cone ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the biggest right circular cylinder ` `float` `cyl(` `float` `r, ` `float` `h) ` `{ ` ` ` ` ` `// radius and height cannot be negative ` ` ` `if` `(r < 0 && h < 0) ` ` ` `return` `-1; ` ` ` ` ` `// radius of right circular cylinder ` ` ` `float` `R = (2 * r) / 3; ` ` ` ` ` `// height of right circular cylinder ` ` ` `float` `H = (2 * h) / 3; ` ` ` ` ` `// volume of right circular cylinder ` ` ` `float` `V = 3.14 * ` `pow` `(R, 2) * H; ` ` ` ` ` `return` `V; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `float` `r = 4, h = 8; ` ` ` `cout << cyl(r, h) << endl; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java Program to find the biggest ` `// right circular cylinder that can ` `// be fit within a right circular cone ` ` ` `import` `java.io.*; ` ` ` `class` `GFG { ` `// Function to find the biggest right circular cylinder ` `static` `double` `cyl(` `double` `r, ` `double` `h) ` `{ ` ` ` ` ` `// radius and height cannot be negative ` ` ` `if` `(r < ` `0` `&& h < ` `0` `) ` ` ` `return` `-` `1` `; ` ` ` ` ` `// radius of right circular cylinder ` ` ` `double` `R = (` `2` `* r) / ` `3` `; ` ` ` ` ` `// height of right circular cylinder ` ` ` `double` `H = (` `2` `* h) / ` `3` `; ` ` ` ` ` `// volume of right circular cylinder ` ` ` `double` `V = ` `3.14` `* Math.pow(R, ` `2` `) * H; ` ` ` ` ` `return` `V; ` `} ` ` ` `// Driver code ` ` ` ` ` `public` `static` `void` `main (String[] args) { ` ` ` ` ` `double` `r = ` `4` `, h = ` `8` `; ` ` ` `System.out.println (cyl(r, h)); ` ` ` `} ` `//This code is contributed by ajit ` `} ` |

*chevron_right*

*filter_none*

## Python 3

# Python 3 Program to find the biggest

# right circular cylinder that can

# be fit within a right circular cone

import math

# Function to find the biggest

# right circular cylinder

def cyl(r, h):

# radius and height cannot

# be negative

if (r < 0 and h < 0):
return -1
# radius of right circular cylinder
R = (2 * r) / 3
# height of right circular cylinder
H = (2 * h) / 3
# volume of right circular cylinder
V = 3.14 * math.pow(R, 2) * H
return V
# Driver code
r = 4; h = 8;
print(cyl(r, h), "\n")
# This code is contributed
# by Akanksha Rai
[tabby title="C#"]

`// C# Program to find the biggest ` `// right circular cylinder that ` `// can be fit within a right circular cone ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to find the biggest ` `// right circular cylinder ` `static` `double` `cyl(` `double` `r, ` `double` `h) ` `{ ` ` ` ` ` `// radius and height cannot ` ` ` `// be negative ` ` ` `if` `(r < 0 && h < 0) ` ` ` `return` `-1; ` ` ` ` ` `// radius of right circular cylinder ` ` ` `double` `R = (2 * r) / 3; ` ` ` ` ` `// height of right circular cylinder ` ` ` `double` `H = (2 * h) / 3; ` ` ` ` ` `// volume of right circular cylinder ` ` ` `double` `V = 3.14 * Math.Pow(R, 2) * H; ` ` ` ` ` `return` `V; ` `} ` ` ` `// Driver code ` `static` `public` `void` `Main () ` `{ ` ` ` `double` `r = 4, h = 8; ` ` ` `Console.WriteLine(cyl(r, h)); ` `} ` `} ` ` ` `// This code is contributed by jit_t ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP Program to find the biggest ` `// right circular cylinder that can ` `// be fit within a right circular cone ` ` ` `// Function to find the biggest ` `// right circular cylinder ` `function` `cyl(` `$r` `, ` `$h` `) ` `{ ` ` ` ` ` `// radius and height cannot ` ` ` `// be negative ` ` ` `if` `(` `$r` `< 0 && ` `$h` `< 0) ` ` ` `return` `-1; ` ` ` ` ` `// radius of right circular cylinder ` ` ` `$R` `= (int)(2 * ` `$r` `) / 3; ` ` ` ` ` `// height of right circular cylinder ` ` ` `$H` `= (int)(2 * ` `$h` `) / 3; ` ` ` ` ` `// volume of right circular cylinder ` ` ` `$V` `= 3.14 * pow(` `$R` `, 2) * ` `$H` `; ` ` ` ` ` `return` `$V` `; ` `} ` ` ` `// Driver code ` `$r` `= 4; ` `$h` `= 8; ` `echo` `cyl(` `$r` `, ` `$h` `); ` ` ` `// This code is contributed by ajit ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

119.087

## Recommended Posts:

- Largest right circular cylinder that can be inscribed within a cone which is in turn inscribed within a cube
- Largest sphere that can be inscribed within a cube which is in turn inscribed within a right circular cone
- Largest right circular cone that can be inscribed within a sphere which is inscribed within a cube
- Largest sphere that can be inscribed in a right circular cylinder inscribed in a frustum
- Largest cube that can be inscribed within a right circular cone
- Largest right circular cone that can be inscribed within a sphere
- Largest cube that can be inscribed within a right circular cylinder
- Largest cone that can be inscribed within a cube
- Largest right circular cylinder within a cube
- Largest right circular cylinder within a frustum
- Volume of largest right circular cylinder within a Sphere
- Largest square that can be inscribed within a hexagon which is inscribed within an equilateral triangle
- Largest ellipse that can be inscribed within a rectangle which in turn is inscribed within a semicircle
- Longest rod that can be inserted within a right circular cylinder
- Volume of biggest sphere within a right circular cylinder

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.