Posted on by Kalkicode
Code Geometric

# Find volume of cuboid

A cuboid, also known as a rectangular prism, is a three-dimensional geometric shape with six rectangular faces. Calculating the volume of a cuboid is essential in various fields such as engineering, architecture, and manufacturing, where objects with cuboidal shapes need to be designed, analyzed, or manufactured.

## Problem Statement

Given the dimensions of a cuboid - length (L), width (W), and height (H), the task is to calculate its volume. The formula for finding the volume of a cuboid is:

Volume = L * W * H

## Example Scenario

Imagine you are designing a water tank in the shape of a cuboid to store a specific amount of water. To ensure that the tank can hold the required volume, you need to calculate the volume of the cuboid.

## Idea to Solve the Problem

To solve this problem, you can follow these steps:

1. Accept the length, width, and height of the cuboid as inputs.
2. Use the formula to calculate the volume of the cuboid.
3. Display the calculated volume.

## Pseudocode

function cuboid_volume(L, W, H):
volume = L * W * H
return volume

main:
L1, W1, H1 = 3.5, 5.5, 7.2
L2, W2, H2 = 2, 3, 5
L3, W3, H3 = 7, 5.4, 5

volume1 = cuboid_volume(L1, W1, H1)
volume2 = cuboid_volume(L2, W2, H2)
volume3 = cuboid_volume(L3, W3, H3)

print("Cuboid [ Length :", L1, "Width :", W1, "Height :", H1, "]")
print("Volume :", volume1)

print("Cuboid [ Length :", L2, "Width :", W2, "Height :", H2, "]")
print("Volume :", volume2)

print("Cuboid [ Length :", L3, "Width :", W3, "Height :", H3, "]")
print("Volume :", volume3)

## Algorithm Explanation

1. Define a function cuboid_volume that takes the length (L), width (W), and height (H) as inputs.
2. Inside the function, use the provided formula to calculate the volume of the cuboid.
3. In the main function, set three test cases with different dimensions (L, W, H).
4. Calculate the volumes for each test case by calling the cuboid_volume function.
5. Display the calculated volumes along with their respective dimensions.

## Code Solution

/*
C Program
Find volume of cuboid
*/
#include <stdio.h>

//Calculate volume of cuboid by length,width and height
void cuboid_volume(double length, double width, double height)
{
printf("\nGiven Length : %lf, Width : %lf , Height : %lf", length, width, height);
// Formula of cuboid volume
// (length * width * height)
// Calculate volume of cuboid
double result = (length * width * height);
//Display result
printf("\nVolume of cuboid : %lf\n", result);
}
int main()
{
//Simple Case
cuboid_volume(3.5, 5.5, 7.2);
cuboid_volume(2, 3, 5);
cuboid_volume(7, 5.4, 5);
return 0;
}

#### Output

Given Length : 3.500000, Width : 5.500000 , Height : 7.200000
Volume of cuboid : 138.600000

Given Length : 2.000000, Width : 3.000000 , Height : 5.000000
Volume of cuboid : 30.000000

Given Length : 7.000000, Width : 5.400000 , Height : 5.000000
Volume of cuboid : 189.000000
// Java Program
// Find volume of cuboid
class Cuboid
{
//Calculate volume of cuboid by length,width and height
void volume(double length, double width, double height)
{
System.out.print("\nGiven Length : " + length + ", Width : " + width + " , Height : " + height);
// Formula of cuboid volume
// (length * width * height)
// Calculate volume of cuboid
double result = (length * width * height);
// Display result
System.out.print("\nVolume of cuboid : " + result + " \n");
}
public static void main(String[] args)
{
Cuboid obj = new Cuboid();
//Simple Case
obj.volume(3.5, 5.5, 7.2);
obj.volume(2, 3, 5);
obj.volume(7, 5.4, 5);
}
}

#### Output

Given Length : 3.5, Width : 5.5 , Height : 7.2
Volume of cuboid : 138.6

Given Length : 2.0, Width : 3.0 , Height : 5.0
Volume of cuboid : 30.0

Given Length : 7.0, Width : 5.4 , Height : 5.0
Volume of cuboid : 189.00000000000003
// C++ Program
// Find volume of cuboid
#include<iostream>

using namespace std;
class Cuboid
{
public:
//Calculate volume of cuboid by length,width and height
void volume(double length, double width, double height)
{
cout << "\nGiven Length : " << length << ", Width : " << width << " , Height : " << height;
// Formula of cuboid volume
// (length * width * height)
// Calculate volume of cuboid
double result = (length * width * height);
cout << "\nVolume of cuboid : " << result << " \n";
}
};
int main()
{
Cuboid obj ;
//Simple Case
obj.volume(3.5, 5.5, 7.2);
obj.volume(2, 3, 5);
obj.volume(7, 5.4, 5);
return 0;
}

#### Output

Given Length : 3.5, Width : 5.5 , Height : 7.2
Volume of cuboid : 138.6

Given Length : 2, Width : 3 , Height : 5
Volume of cuboid : 30

Given Length : 7, Width : 5.4 , Height : 5
Volume of cuboid : 189
// C# Program
// Find volume of cuboid
using System;
class Cuboid
{
//Calculate volume of cuboid by length,width and height
void volume(double length, double width, double height)
{
Console.Write("\nGiven Length : " + length + ", Width : " + width + " , Height : " + height);
// Formula of cuboid volume
// (length * width * height)
// Calculate volume of cuboid
double result = (length * width * height);
Console.Write("\nVolume of cuboid : " + result + " \n");
}
public static void Main(String[] args)
{
Cuboid obj = new Cuboid();
//Simple Case
obj.volume(3.5, 5.5, 7.2);
obj.volume(2, 3, 5);
obj.volume(7, 5.4, 5);
}
}

#### Output

Given Length : 3.5, Width : 5.5 , Height : 7.2
Volume of cuboid : 138.6

Given Length : 2, Width : 3 , Height : 5
Volume of cuboid : 30

Given Length : 7, Width : 5.4 , Height : 5
Volume of cuboid : 189
<?php
// Php Program
// Find volume of cuboid
class Cuboid
{
//Calculate volume of cuboid by length,width and height
function volume(\$length, \$width, \$height)
{
echo "\nGiven Length : ". \$length .", Width : ". \$width ." , Height : ". \$height;
// Formula of cuboid volume
// (length * width * height)
// Calculate volume of cuboid
\$result = (\$length * \$width * \$height);
echo "\nVolume of cuboid : ". \$result ." \n";
}
}

function main()
{
\$obj = new Cuboid();
//Simple Case
\$obj->volume(3.5, 5.5, 7.2);
\$obj->volume(2, 3, 5);
\$obj->volume(7, 5.4, 5);
}
main();

#### Output

Given Length : 3.5, Width : 5.5 , Height : 7.2
Volume of cuboid : 138.6

Given Length : 2, Width : 3 , Height : 5
Volume of cuboid : 30

Given Length : 7, Width : 5.4 , Height : 5
Volume of cuboid : 189
// Node Js Program
// Find volume of cuboid
class Cuboid
{
//Calculate volume of cuboid by length,width and height
volume(length, width, height)
{
process.stdout.write("\nGiven Length : " + length + ", Width : " + width + " , Height : " + height);
// Formula of cuboid volume
// (length * width * height)
// Calculate volume of cuboid
var result = (length * width * height);
process.stdout.write("\nVolume of cuboid : " + result + " \n");
}
}

function main()
{
var obj = new Cuboid();
//Simple Case
obj.volume(3.5, 5.5, 7.2);
obj.volume(2, 3, 5);
obj.volume(7, 5.4, 5);
}
main();

#### Output

Given Length : 3.5, Width : 5.5 , Height : 7.2
Volume of cuboid : 138.6

Given Length : 2, Width : 3 , Height : 5
Volume of cuboid : 30

Given Length : 7, Width : 5.4 , Height : 5
Volume of cuboid : 189.00000000000003
#  Python 3 Program
#  Find volume of cuboid
class Cuboid :
# Calculate volume of cuboid by length,width and height
def volume(self, length, width, height) :
print("\nGiven Length : ", length ,", Width : ", width ," , Height : ", height, end = "")
#  Formula of cuboid volume
#  (length * width * height)
#  Calculate volume of cuboid
result = (length * width * height)
print("\nVolume of cuboid : ", result ," \n", end = "")

def main() :
obj = Cuboid()
# Simple Case
obj.volume(3.5, 5.5, 7.2)
obj.volume(2, 3, 5)
obj.volume(7, 5.4, 5)

if __name__ == "__main__": main()

#### Output

Given Length :  3.5 , Width :  5.5  , Height :  7.2
Volume of cuboid :  138.6

Given Length :  2 , Width :  3  , Height :  5
Volume of cuboid :  30

Given Length :  7 , Width :  5.4  , Height :  5
Volume of cuboid :  189.00000000000003
#  Ruby Program
#  Find volume of cuboid
class Cuboid

# Calculate volume of cuboid by length,width and height
def volume(length, width, height)

print("\nGiven Length : ", length ,", Width : ", width ," , Height : ", height)
#  Formula of cuboid volume
#  (length * width * height)
#  Calculate volume of cuboid
result = (length * width * height)
#  Display result
print("\nVolume of cuboid : ", result ," \n")
end
end
def main()

obj = Cuboid.new()
# Simple Case
obj.volume(3.5, 5.5, 7.2)
obj.volume(2, 3, 5)
obj.volume(7, 5.4, 5)
end
main()

#### Output

Given Length : 3.5, Width : 5.5 , Height : 7.2
Volume of cuboid : 138.6

Given Length : 2, Width : 3 , Height : 5
Volume of cuboid : 30

Given Length : 7, Width : 5.4 , Height : 5
Volume of cuboid : 189.00000000000003
// Scala Program
// Find volume of cuboid
class Cuboid
{
//Calculate volume of cuboid by length,width and height
def volume(length: Double, width: Double, height: Double): Unit = {
print("\nGiven Length : " + length + ", Width : " + width + " , Height : " + height);
// Formula of cuboid volume
// (length * width * height)
// Calculate volume of cuboid
var result: Double = (length * width * height);
// Display result
print("\nVolume of cuboid : " + result + " \n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: Cuboid = new Cuboid();
//Simple Case
obj.volume(3.5, 5.5, 7.2);
obj.volume(2, 3, 5);
obj.volume(7, 5.4, 5);
}
}

#### Output

Given Length : 3.5, Width : 5.5 , Height : 7.2
Volume of cuboid : 138.6

Given Length : 2.0, Width : 3.0 , Height : 5.0
Volume of cuboid : 30.0

Given Length : 7.0, Width : 5.4 , Height : 5.0
Volume of cuboid : 189.00000000000003
// Swift Program
// Find volume of cuboid
class Cuboid
{
//Calculate volume of cuboid by length,width and height
func volume(_ length: Double, _ width: Double, _ height: Double)
{
print("\nGiven Length : ", length ,", Width : ", width ," , Height : ", height, terminator: "");
// Formula of cuboid volume
// (length * width * height)
// Calculate volume of cuboid
let result: Double = (length * width * height);
print("\nVolume of cuboid : ", result ," \n", terminator: "");
}
}
func main()
{
let obj: Cuboid = Cuboid();
//Simple Case
obj.volume(3.5, 5.5, 7.2);
obj.volume(2, 3, 5);
obj.volume(7, 5.4, 5);
}
main();

#### Output

Given Length :  3.5 , Width :  5.5  , Height :  7.2
Volume of cuboid :  138.6

Given Length :  2.0 , Width :  3.0  , Height :  5.0
Volume of cuboid :  30.0

Given Length :  7.0 , Width :  5.4  , Height :  5.0
Volume of cuboid :  189.0

## Output Explanation

The code calculates the volume for each test case and displays the results. For a cuboid with dimensions (L, W, H) = (3.5, 5.5, 7.2), the volume is 138.600000. Similarly, for dimensions (L, W, H) = (2, 3, 5) and (L, W, H) = (7, 5.4, 5), the volumes are 30.000000 and 189.000000, respectively.

## Time Complexity

The time complexity of this code is constant O(1) because the calculations involve basic arithmetic operations (multiplication). These operations are computed in constant time regardless of the input size. The program performs a fixed number of operations for each test case, making it efficient.

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

Categories
Relative Post