# Find maximum volume of a cuboid from the given perimeter and area

Given a perimeter P and area A, the task is to calculate the maximum volume that can be made in form of cuboid from the given perimeter and surface area.

**Examples :**

Input:P = 24, A = 24Output:8Input:P = 20, A = 14Output:3

**Approach:** For a given perimeter of cuboid we have P = 4(l+b+h) —(i),

for given area of cuboid we have A = 2 (lb+bh+lh) —(ii).

Volume of cuboid is V = lbh

Volume is dependent on 3 variables l, b, h. Lets make it dependent on only length.

as V = lbh,

=> V = l (A/2-(lb+lh)) {from equation (ii)}

=> V = lA/2 – l^{2}(b+h)

=> V = lA/2 – l^{2}(P/4-l) {from equation (i)}

=> V = lA/2 – l^{2}P/4 + l^{3}—-(iii)Now differentiate

Vw.r.tlfor finding maximum of volume.

dV/dl = A/2 – lP/2 + 3l^{2}

After solving the quadratic inlwe havel = (P – (P^{2}-24A)) / 12

Substituting value oflin (iii), we can easily find the maximum volume.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// function to return maximum volume ` `float` `maxVol(` `float` `P, ` `float` `A) ` `{ ` ` ` `// calculate length ` ` ` `float` `l = (P - ` `sqrt` `(P * P - 24 * A)) / 12; ` ` ` ` ` `// calculate volume ` ` ` `float` `V = l * (A / 2.0 - l * (P / 4.0 - l)); ` ` ` ` ` `// return result ` ` ` `return` `V; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `float` `P = 20, A = 16; ` ` ` `cout << maxVol(P, A); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the above approach ` `import` `java.util.*; ` ` ` `class` `Geeks { ` ` ` `// function to return maximum volume ` `static` `float` `maxVol(` `float` `P, ` `float` `A) ` `{ ` ` ` `// calculate length ` ` ` `float` `l = (` `float` `)(P - Math.sqrt(P * P - ` `24` `* A)) / ` `12` `; ` ` ` ` ` `// calculate volume ` ` ` `float` `V = (` `float` `)(l * (A / ` `2.0` `- l * (P / ` `4.0` `- l))); ` ` ` ` ` `// return result ` ` ` `return` `V; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String args[]) ` `{ ` ` ` `float` `P = ` `20` `, A = ` `16` `; ` ` ` `System.out.println(maxVol(P, A)); ` `} ` `} ` ` ` `// This code is contributed by Kirti_Mangal ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the ` `# above approach ` `from` `math ` `import` `sqrt ` ` ` `# function to return maximum volume ` `def` `maxVol(P, A): ` ` ` ` ` `# calculate length ` ` ` `l ` `=` `(P ` `-` `sqrt(P ` `*` `P ` `-` `24` `*` `A)) ` `/` `12` ` ` ` ` `# calculate volume ` ` ` `V ` `=` `l ` `*` `(A ` `/` `2.0` `-` `l ` `*` `(P ` `/` `4.0` `-` `l)) ` ` ` ` ` `# return result ` ` ` `return` `V ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `P ` `=` `20` ` ` `A ` `=` `16` ` ` `print` `(maxVol(P, A)) ` ` ` `# This code is contributed ` `# by Surendra_Gangwar ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the above approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// function to return maximum volume ` `static` `float` `maxVol(` `float` `P, ` `float` `A) ` `{ ` ` ` `// calculate length ` ` ` `float` `l = (` `float` `)(P - ` ` ` `Math.Sqrt(P * P - 24 * A)) / 12; ` ` ` ` ` `// calculate volume ` ` ` `float` `V = (` `float` `)(l * (A / 2.0 - l * ` ` ` `(P / 4.0 - l))); ` ` ` ` ` `// return result ` ` ` `return` `V; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main() ` `{ ` ` ` `float` `P = 20, A = 16; ` ` ` `Console.WriteLine(maxVol(P, A)); ` `} ` `} ` ` ` `// This code is contributed ` `// by Akanksha Rai ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of the above approach ` ` ` `// function to return maximum volume ` `function` `maxVol(` `$P` `, ` `$A` `) ` `{ ` ` ` `// calculate length ` ` ` `$l` `= (` `$P` `- sqrt(` `$P` `* ` `$P` `- 24 * ` `$A` `)) / 12; ` ` ` ` ` `// calculate volume ` ` ` `$V` `= ` `$l` `* (` `$A` `/ 2.0 - ` `$l` `* ` ` ` `(` `$P` `/ 4.0 - ` `$l` `)); ` ` ` ` ` `// return result ` ` ` `return` `$V` `; ` `} ` ` ` `// Driver code ` `$P` `= 20; ` `$A` `= 16; ` `echo` `maxVol(` `$P` `, ` `$A` `); ` ` ` `// This code is contributed by mits ` `?> ` |

*chevron_right*

*filter_none*

## Recommended Posts:

- Program for Volume and Surface Area of Cuboid
- Maximum area of rectangle possible with given perimeter
- Program to find the Area and Perimeter of a Semicircle
- Maximize volume of cuboid with given sum of sides
- Program to find the Area and Volume of Icosahedron
- Program to find volume and surface area of pentagonal prism
- Percentage increase in the volume of cuboid if length, breadth and height are increased by fixed percentages
- Python | Percentage increase in the total surface area of the cuboid
- Calculate Volume, Curved Surface Area and Total Surface Area Of Cylinder
- Program for Area And Perimeter Of Rectangle
- Perimeter and Area of Varignon's Parallelogram
- Program to calculate area and perimeter of Trapezium
- Program to calculate area and perimeter of equilateral triangle
- Program to calculate area and perimeter of a rhombus whose diagonals are given
- Program to calculate the Area and Perimeter of Incircle of an Equilateral Triangle

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.