Given the sum of length, breadth, and height of a cuboid say S and the task is to get the maximum volume of a cuboid such that the sum of the side is S.
Examples:
Example1:
Input:
Given sum = 5
Output:
The maximum volume of a cuboid such that the sum of the side { 5 } = 4Example2:
Input:
Given sum = 11
Output:
The maximum volume of a cuboid such that the sum of the side { 11 } = 48Program for Maximize Volume of Cuboid with Given Sum of Sides in Python
Below are the ways to get the maximum volume of a cuboid such that the sum of the side is S in python:
Method #1: Using For loop (Static Input)
Approach:
- Give the number (sum) as static input and store it in a variable.
- Create a function to say Maximum_volume() which takes the given number as an argument and returns the maximum volume of a cuboid such that the sum of the side is the given sum.
- Inside the function, take a variable to say maxim_vol and initialize its value to 0.
- Take another variable (for length)Â say a and initialize its value to 1.
- Loop till the given sum -1 using the for loop.
- Take another variable (for breadth) say b and initialize its value to 1.
- Loop till the given sum using another nested for loop.
- Subtract the above variables a, b values from the given sum and store it in a variable say c (for height).
- Multiply the above a, b, c values and store them in another variable.
- Get the maximum value from the above-initialized maxim_vol and above multiplication result using the max() function and store it in another variable.
- Return the above result which is the maximum volume of a cuboid.
- Pass the given number as an argument to the Maximum_volume() function and print it.
- The Exit of the Program.
Below is the implementation:
# Create a function to say Maximum_volume() which takes the given number as an
# argument and returns the maximum volume of a cuboid such that the sum of the side
# is the given sum.
def Maximum_volume(gvn_sum):
# Inside the function, take a variable and initialize its value to 0.
maxim_vol = 0
# Take another variable (for length) say a and initialize its value to 1.
a = 1
# Loop till the given sum -1 using the for loop.
for a in range(gvn_sum - 1):
# Take another variable (for breadth) say b and initialize its value to 1.
b = 1
# Loop till the given sum using the another nested for loop.
for b in range(gvn_sum):
# Subtract the above variables a, b values from the given sum and store
# it in a variable say c.
c = gvn_sum - a - b
# Multiply the above a, b, c values and store them in another variable.
mult = a * b * c
# Get the maximum value from the above-initialized maxim_vol and above
# multiplication result using the max() function and store it in
# another variable.
maxim_vol = max(maxim_vol, mult)
# Return the above result which is the maximum volume of a cuboid.
return maxim_vol
# Give the number (sum) as static input and store it in a variable.
gvn_sum = 5
# Pass the given number as an argument to the Maximum_volume() function
# and print it.
print("The maximum volume of a cuboid such that the sum of the side {", gvn_sum, "} = ", Maximum_volume(
gvn_sum))
Output:
The maximum volume of a cuboid such that the sum of the side { 5 } = 4Method #2: Using For loop (User Input)
Approach:
- Give the number (sum) as user input using the int(input()) function and store it in a variable.
- Create a function to say Maximum_volume() which takes the given number as an argument and returns the maximum volume of a cuboid such that the sum of the side is the given sum.
- Inside the function, take a variable to say maxim_vol and initialize its value to 0.
- Take another variable (for length)Â say a and initialize its value to 1.
- Loop till the given sum -1 using the for loop.
- Take another variable (for breadth) say b and initialize its value to 1.
- Loop till the given sum using another nested for loop.
- Subtract the above variables a, b values from the given sum and store it in a variable say c (for height).
- Multiply the above a, b, c values and store them in another variable.
- Get the maximum value from the above-initialized maxim_vol and above multiplication result using the max() function and store it in another variable.
- Return the above result which is the maximum volume of a cuboid.
- Pass the given number as an argument to the Maximum_volume() function and print it.
- The Exit of the Program.
Below is the implementation:
# Create a function to say Maximum_volume() which takes the given number as an
# argument and returns the maximum volume of a cuboid such that the sum of the side
# is the given sum.
def Maximum_volume(gvn_sum):
# Inside the function, take a variable and initialize its value to 0.
maxim_vol = 0
# Take another variable (for length)Â say a and initialize its value to 1.
a = 1
# Loop till the given sum -1 using the for loop.
for a in range(gvn_sum - 1):
# Take another variable (for breadth) say b and initialize its value to 1.
b = 1
# Loop till the given sum using the another nested for loop.
for b in range(gvn_sum):
# Subtract the above variables a, b values from the given sum and store
# it in a variable say c.(for height)
c = gvn_sum - a - b
# Multiply the above a, b, c values and store them in another variable.
mult = a * b * c
# Get the maximum value from the above-initialized maxim_vol and above
# multiplication result using the max() function and store it in
# another variable.
maxim_vol = max(maxim_vol, mult)
# Return the above result which is the maximum volume of a cuboid.
return maxim_vol
# Give the number (sum) as user input using the int(input()) function and
# store it in a variable.
gvn_sum = int(input("Enter some random number = "))
# Pass the given number as an argument to the Maximum_volume() function
# and print it.
print("The maximum volume of a cuboid such that the sum of the side {", gvn_sum, "} = ", Maximum_volume(
gvn_sum))
Output:
Enter some random number = 11
The maximum volume of a cuboid such that the sum of the side { 11 } = 48