Posted on by Kalkicode
Code Geometric

# Find surface area of cuboid

A cuboid, also known as a rectangular prism, is a three-dimensional geometric shape with six rectangular faces. Calculating the surface area of a cuboid is important in various fields such as architecture, engineering, and packaging, 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 surface area. The formula for finding the surface area of a cuboid is:

Surface Area = 2 * (L * W + L * H + W * H)

## Example Scenario

Imagine you are designing a gift box in the shape of a cuboid for a special occasion. You want to decorate each face of the box with unique patterns. To determine the amount of wrapping paper needed, you need to calculate the total surface area 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 surface area of the cuboid.
3. Display the calculated surface area.

## Pseudocode

``````function cuboid_surface_area(L, W, H):
surface_area = 2 * (L * W + L * H + W * H)
return surface_area

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

area1 = cuboid_surface_area(L1, W1, H1)
area2 = cuboid_surface_area(L2, W2, H2)
area3 = cuboid_surface_area(L3, W3, H3)

print("Cuboid [ Length :", L1, "Width :", W1, "Height :", H1, "]")
print("Surface Area :", area1)

print("Cuboid [ Length :", L2, "Width :", W2, "Height :", H2, "]")
print("Surface Area :", area2)

print("Cuboid [ Length :", L3, "Width :", W3, "Height :", H3, "]")
print("Surface Area :", area3)``````

## Algorithm Explanation

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

## Code Solution

``````/*
C Program
Find surface area of cuboid
*/
#include <stdio.h>

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

#### Output

``````Given Length : 3.500000, Width : 5.500000 , Height : 7.200000
Surface area of cuboid : 168.100000

Given Length : 2.000000, Width : 3.000000 , Height : 5.000000
Surface area of cuboid : 62.000000

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

#### Output

``````Given Length : 3.5, Width : 5.5 , Height : 7.2
Surface area of cuboid : 168.10000000000002

Given Length : 2.0, Width : 3.0 , Height : 5.0
Surface area of cuboid : 62.0

Given Length : 7.0, Width : 5.4 , Height : 5.0
Surface area of cuboid : 199.60000000000002``````
``````// C++ Program
// Find surface area of cuboid
#include<iostream>
using namespace std;
class Cuboid
{
public:
//Calculate surface area of cuboid by length,width and height
void cuboid_surface_area(double length, double width, double height)
{
cout << "\nGiven Length : " << length << ", Width : " << width << " , Height : " << height;
// Formula of cuboid surface area
// (2*length*width)+(2*length*height)+(2*width*height)
// Calculate surface area of cuboid
double result = (2 * length * width) + (2 * length * height) + (2 * width * height);
cout << "\nSurface area of cuboid : " << result << " \n";
}
};
int main()
{
Cuboid obj;
//Simple Case
obj.cuboid_surface_area(3.5, 5.5, 7.2);
obj.cuboid_surface_area(2, 3, 5);
obj.cuboid_surface_area(7, 5.4, 5);
return 0;
}``````

#### Output

``````Given Length : 3.5, Width : 5.5 , Height : 7.2
Surface area of cuboid : 168.1

Given Length : 2, Width : 3 , Height : 5
Surface area of cuboid : 62

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

#### Output

``````Given Length : 3.5, Width : 5.5 , Height : 7.2
Surface area of cuboid : 168.1

Given Length : 2, Width : 3 , Height : 5
Surface area of cuboid : 62

Given Length : 7, Width : 5.4 , Height : 5
Surface area of cuboid : 199.6``````
``````<?php
// Php Program
// Find surface area of cuboid
class Cuboid
{
//Calculate surface area of cuboid by length,width and height
function cuboid_surface_area(\$length, \$width, \$height)
{
echo "\nGiven Length : ". \$length .", Width : ". \$width ." , Height : ". \$height;
// Formula of cuboid surface area
// (2*length*width)+(2*length*height)+(2*width*height)
// Calculate surface area of cuboid
\$result = (2 * \$length * \$width) + (2 * \$length * \$height) + (2 * \$width * \$height);
echo "\nSurface area of cuboid : ". \$result ." \n";
}
}

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

#### Output

``````Given Length : 3.5, Width : 5.5 , Height : 7.2
Surface area of cuboid : 168.1

Given Length : 2, Width : 3 , Height : 5
Surface area of cuboid : 62

Given Length : 7, Width : 5.4 , Height : 5
Surface area of cuboid : 199.6``````
``````// Node Js Program
// Find surface area of cuboid
class Cuboid
{
//Calculate surface area of cuboid by length,width and height
cuboid_surface_area(length, width, height)
{
process.stdout.write("\nGiven Length : " + length + ", Width : " + width + " , Height : " + height);
// Formula of cuboid surface area
// (2*length*width)+(2*length*height)+(2*width*height)
// Calculate surface area of cuboid
var result = (2 * length * width) + (2 * length * height) + (2 * width * height);
process.stdout.write("\nSurface area of cuboid : " + result + " \n");
}
}

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

#### Output

``````Given Length : 3.5, Width : 5.5 , Height : 7.2
Surface area of cuboid : 168.10000000000002

Given Length : 2, Width : 3 , Height : 5
Surface area of cuboid : 62

Given Length : 7, Width : 5.4 , Height : 5
Surface area of cuboid : 199.60000000000002``````
``````#  Python 3 Program
#  Find surface area of cuboid
class Cuboid :
# Calculate surface area of cuboid by length,width and height
def cuboid_surface_area(self, length, width, height) :
print("\nGiven Length : ", length ,", Width : ", width ," , Height : ", height, end = "")
#  Formula of cuboid surface area
#  (2*length*width)+(2*length*height)+(2*width*height)
#  Calculate surface area of cuboid
result = (2 * length * width) + (2 * length * height) + (2 * width * height)
print("\nSurface area of cuboid : ", result ," \n", end = "")

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

if __name__ == "__main__": main()``````

#### Output

``````Given Length :  3.5 , Width :  5.5  , Height :  7.2
Surface area of cuboid :  168.10000000000002

Given Length :  2 , Width :  3  , Height :  5
Surface area of cuboid :  62

Given Length :  7 , Width :  5.4  , Height :  5
Surface area of cuboid :  199.60000000000002``````
``````#  Ruby Program
#  Find surface area of cuboid
class Cuboid

# Calculate surface area of cuboid by length,width and height
def cuboid_surface_area(length, width, height)

print("\nGiven Length : ", length ,", Width : ", width ," , Height : ", height)
#  Formula of cuboid surface area
#  (2*length*width)+(2*length*height)+(2*width*height)
#  Calculate surface area of cuboid
result = (2 * length * width) + (2 * length * height) + (2 * width * height)
# Display result
print("\nSurface area of cuboid : ", result ," \n")
end
end
def main()

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

#### Output

``````Given Length : 3.5, Width : 5.5 , Height : 7.2
Surface area of cuboid : 168.10000000000002

Given Length : 2, Width : 3 , Height : 5
Surface area of cuboid : 62

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

#### Output

``````Given Length : 3.5, Width : 5.5 , Height : 7.2
Surface area of cuboid : 168.10000000000002

Given Length : 2.0, Width : 3.0 , Height : 5.0
Surface area of cuboid : 62.0

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

#### Output

``````Given Length :  3.5 , Width :  5.5  , Height :  7.2
Surface area of cuboid :  168.1

Given Length :  2.0 , Width :  3.0  , Height :  5.0
Surface area of cuboid :  62.0

Given Length :  7.0 , Width :  5.4  , Height :  5.0
Surface area of cuboid :  199.6``````

## Output Explanation

The code calculates the surface area for each test case and displays the results. For a cuboid with dimensions (L, W, H) = (3.5, 5.5, 7.2), the surface area is approximately 168.100000. Similarly, for dimensions (L, W, H) = (2, 3, 5) and (L, W, H) = (7, 5.4, 5), the surface areas are approximately 62.000000 and 199.600000, respectively.

## Time Complexity

The time complexity of this code is constant O(1) because the calculations involve basic arithmetic operations (multiplication and addition). 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