Java Program to Find Maximum Volume of Cuboid that can be Achieved with Given Sum of Sides

In the previous article, we have seen Java Program to Find Surface Area of Hemisphere

In this article we are going to see how to find the maximum volume of cuboid that can be achieved with given sum of sides using Java programming language.

Java Program to Find Maximum Volume of Cuboid that can be Achieved with Given Sum of Sides

Before Jumping into the program directly let’s see how we can Find Maximum Volume of Cuboid that can be Achieved with Given Sum of Sides..

Explanation:

Let the sum of length, breadth and height be “S”, of a cuboid.

The task is to find the maximum volume that can be achieved so that sum of side is S.
Volume of a cuboid = length * breadth * height

Example:

let S = 8

All possible edge size be:
[1, 1, 6], volume = 6
[1, 2, 5], volume = 10
[1, 3, 4], volume = 12
[2, 2, 4], volume = 16
[2, 3, 3], volume = 18
Max volume out of those possible edge sizes = 18

Let’s see different ways to find the maximum volume of cuboid that can be achieved with given sum of sides.

Method-1: Java Program to Find Maximum Volume of Cuboid that can be Achieved with Given Sum of Sides By Using Static Value

Approach:

  • Declare an integer variable say ‘s’ and assign the value to it, which holds the sum of the sides of the cuboid.
  • Call a method maxvolume(int s) where find the max volume of the cuboid using the formula maxvalue = Math.max(maxvalue, i * j * k) where i, j, k are the local variable to store the value of length, breadth, height of the cuboid.
  • Print the result.

Program:

import java.io.*;
public class Main
{
    static int maxvolume(int s) 
    { 
        int maxvalue = 0; 
        // for length
        for (int i = 1; i <= s - 2; i++)  
        {
             // for breadth
            for (int j = 1; j <= s - 1; j++)
            { 
                // for height
                int k = s - i - j;   
                // calculating maximum volume. 
                maxvalue = Math.max(maxvalue, i * j * k); 
            } 
        }       
        return maxvalue; 
    } 
    // Driver function 
    public static void main (String[] args) 
    { 
        int s = 8; 
        System.out.println("The max vol of the cuboid is: " + maxvolume(s)); 
    } 
}
Output:

The max vol of the cuboid is: 18

Method-2: Java Program to Find Maximum Volume of Cuboid that can be Achieved with Given Sum of Sides By Using User Input Value

Approach:

  • Declare an integer variable say ‘s’ and take it’s value as user input, which holds the sum of the sides of the cuboid.
  • Call a method maxvolume(int s) where find the max volume of the cuboid using the formula maxvalue = Math.max(maxvalue, i * j * k) where i, j, k are the local variable to store the value of length, breadth, height of the cuboid.
  • Print the result.

Program:

import java.util.*;
public class Main
{
    static int maxvolume(int s) 
    { 
        int maxvalue = 0; 
        // for length
        for (int i = 1; i <= s - 2; i++)  
        {
             // for breadth
            for (int j = 1; j <= s - 1; j++)
            { 
                // for height
                int k = s - i - j;   
                // calculating maximum volume. 
                maxvalue = Math.max(maxvalue, i * j * k); 
            } 
        }       
        return maxvalue; 
    } 
    // Driver function 
    public static void main (String[] args) 
    { 
        //Scanner class object created
        Scanner sc = new Scanner(System.in);
        //taking input of sum value of all the sides of cuboid
        System.out.println("Enter the sum of sides of the cuboid: ");
        int s = sc.nextInt(); 
        System.out.println("The max vol of the cuboid is: " + maxvolume(s)); 
 
    } 
}
Output:

Enter the sum of sides of the cuboid: 
18
The max vol of the cuboid is: 216

Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.

Related Java Articles: