{"id":23683,"date":"2021-10-03T20:26:56","date_gmt":"2021-10-03T14:56:56","guid":{"rendered":"https:\/\/python-programs.com\/?p=23683"},"modified":"2021-11-22T18:33:26","modified_gmt":"2021-11-22T13:03:26","slug":"python-program-for-maximize-volume-of-cuboid-with-given-sum-of-sides","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-for-maximize-volume-of-cuboid-with-given-sum-of-sides\/","title":{"rendered":"Python Program for Maximize Volume of Cuboid with Given Sum of Sides"},"content":{"rendered":"
Given the sum of length, breadth, and height of a cuboid say S<\/strong> and the task is to get the maximum volume of a cuboid such that the sum of the side is S<\/strong>.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to get the maximum volume of a cuboid such that the sum of the side is S <\/strong>in python:<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n 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 …<\/p>\nGiven sum = 5<\/pre>\n
The maximum volume of a cuboid such that the sum of the side { 5 } = 4<\/pre>\n
Given sum = 11<\/pre>\n
The maximum volume of a cuboid such that the sum of the side { 11 } = 48<\/pre>\n
Program for Maximize Volume of Cuboid with Given Sum of Sides\u00a0in Python<\/h2>\n
\n
Method #1: Using For loop (Static Input)<\/h3>\n
\n
# Create a function to say Maximum_volume() which takes the given number as an\r\n# argument and returns the maximum volume of a cuboid such that the sum of the side\r\n# is the given sum.\r\n\r\n\r\n\r\ndef Maximum_volume(gvn_sum):\r\n # Inside the function, take a variable and initialize its value to 0.\r\n maxim_vol = 0\r\n\r\n # Take another variable (for length) say a and initialize its value to 1.\r\n a = 1\r\n # Loop till the given sum -1 using the for loop.\r\n for a in range(gvn_sum - 1):\r\n # Take another variable (for breadth) say b and initialize its value to 1.\r\n b = 1\r\n\r\n # Loop till the given sum using the another nested for loop.\r\n for b in range(gvn_sum):\r\n # Subtract the above variables a, b values from the given sum and store\r\n # it in a variable say c.\r\n c = gvn_sum - a - b\r\n # Multiply the above a, b, c values and store them in another variable.\r\n\r\n mult = a * b * c\r\n # Get the maximum value from the above-initialized maxim_vol and above\r\n # multiplication result using the max() function and store it in\r\n # another variable.\r\n maxim_vol = max(maxim_vol, mult)\r\n # Return the above result which is the maximum volume of a cuboid.\r\n return maxim_vol\r\n\r\n\r\n# Give the number (sum) as static input and store it in a variable.\r\ngvn_sum = 5\r\n# Pass the given number as an argument to the Maximum_volume() function\r\n# and print it.\r\nprint(\"The maximum volume of a cuboid such that the sum of the side {\", gvn_sum, \"} = \", Maximum_volume(\r\n gvn_sum))\r\n<\/pre>\n
The maximum volume of a cuboid such that the sum of the side { 5 } = 4<\/pre>\n
Method #2: Using For loop (User Input)<\/h3>\n
\n
# Create a function to say Maximum_volume() which takes the given number as an\r\n# argument and returns the maximum volume of a cuboid such that the sum of the side\r\n# is the given sum.\r\n\r\ndef Maximum_volume(gvn_sum):\r\n # Inside the function, take a variable and initialize its value to 0.\r\n maxim_vol = 0\r\n\r\n # Take another variable (for length)\u00a0say a and initialize its value to 1.\r\n a = 1\r\n # Loop till the given sum -1 using the for loop.\r\n for a in range(gvn_sum - 1):\r\n # Take another variable (for breadth) say b and initialize its value to 1.\r\n b = 1\r\n\r\n # Loop till the given sum using the another nested for loop.\r\n for b in range(gvn_sum):\r\n # Subtract the above variables a, b values from the given sum and store\r\n # it in a variable say c.(for height)\r\n c = gvn_sum - a - b\r\n # Multiply the above a, b, c values and store them in another variable.\r\n\r\n mult = a * b * c\r\n # Get the maximum value from the above-initialized maxim_vol and above\r\n # multiplication result using the max() function and store it in\r\n # another variable.\r\n maxim_vol = max(maxim_vol, mult)\r\n # Return the above result which is the maximum volume of a cuboid.\r\n return maxim_vol\r\n\r\n\r\n# Give the number (sum) as user input using the int(input()) function and \r\n# store it in a variable.\r\ngvn_sum = int(input(\"Enter some random number = \"))\r\n# Pass the given number as an argument to the Maximum_volume() function\r\n# and print it.\r\nprint(\"The maximum volume of a cuboid such that the sum of the side {\", gvn_sum, \"} = \", Maximum_volume(\r\n gvn_sum))\r\n<\/pre>\n
Enter some random number = 11\r\nThe maximum volume of a cuboid such that the sum of the side { 11 } = 48<\/pre>\n","protected":false},"excerpt":{"rendered":"