Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C# Program to Find Volume, Perimeter and Surface Area of Cuboid
A cuboid is a three-dimensional geometric shape with six rectangular faces, where opposite faces are equal in dimensions. It has three dimensions: length, breadth (width), and height. We can calculate the volume, perimeter, and surface area of a cuboid using specific mathematical formulas.
Formulas
The following formulas are used to calculate the properties of a cuboid
Volume of a Cuboid
Volume = length × breadth × height
Perimeter of a Cuboid
Perimeter = 4 × (length + breadth + height)
Surface Area of a Cuboid
Surface Area = 2 × (length × breadth + breadth × height + height × length)
Where
-
Length: The horizontal dimension of the cuboid
-
Breadth: The width or depth of the cuboid
-
Height: The vertical dimension of the cuboid
Using Direct Formula Calculation
This approach directly applies the mathematical formulas to calculate the volume, perimeter, and surface area of a cuboid
using System;
class Program {
static void Main() {
double length = 6;
double breadth = 4;
double height = 3;
double volume = length * breadth * height;
double perimeter = 4 * (length + breadth + height);
double surfaceArea = 2 * (length * breadth + breadth * height + height * length);
Console.WriteLine($"Cuboid Dimensions: Length = {length}, Breadth = {breadth}, Height = {height}");
Console.WriteLine($"Volume: {volume} cubic units");
Console.WriteLine($"Perimeter: {perimeter} units");
Console.WriteLine($"Surface Area: {surfaceArea} square units");
}
}
The output of the above code is
Cuboid Dimensions: Length = 6, Breadth = 4, Height = 3 Volume: 72 cubic units Perimeter: 52 units Surface Area: 108 square units
Using Separate Functions
We can create separate functions for each calculation to improve code organization and reusability
using System;
class Program {
static double CalculateVolume(double length, double breadth, double height) {
return length * breadth * height;
}
static double CalculatePerimeter(double length, double breadth, double height) {
return 4 * (length + breadth + height);
}
static double CalculateSurfaceArea(double length, double breadth, double height) {
return 2 * (length * breadth + breadth * height + height * length);
}
static void Main() {
double length = 8;
double breadth = 5;
double height = 4;
double volume = CalculateVolume(length, breadth, height);
double perimeter = CalculatePerimeter(length, breadth, height);
double surfaceArea = CalculateSurfaceArea(length, breadth, height);
Console.WriteLine($"Cuboid Dimensions: Length = {length}, Breadth = {breadth}, Height = {height}");
Console.WriteLine($"Volume: {volume} cubic units");
Console.WriteLine($"Perimeter: {perimeter} units");
Console.WriteLine($"Surface Area: {surfaceArea} square units");
}
}
The output of the above code is
Cuboid Dimensions: Length = 8, Breadth = 5, Height = 4 Volume: 160 cubic units Perimeter: 68 units Surface Area: 164 square units
Using Object-Oriented Approach
We can create a Cuboid class to encapsulate the dimensions and calculations
using System;
class Cuboid {
private double length, breadth, height;
public Cuboid(double length, double breadth, double height) {
this.length = length;
this.breadth = breadth;
this.height = height;
}
public double GetVolume() {
return length * breadth * height;
}
public double GetPerimeter() {
return 4 * (length + breadth + height);
}
public double GetSurfaceArea() {
return 2 * (length * breadth + breadth * height + height * length);
}
public void DisplayResults() {
Console.WriteLine($"Cuboid Dimensions: Length = {length}, Breadth = {breadth}, Height = {height}");
Console.WriteLine($"Volume: {GetVolume()} cubic units");
Console.WriteLine($"Perimeter: {GetPerimeter()} units");
Console.WriteLine($"Surface Area: {GetSurfaceArea()} square units");
}
}
class Program {
static void Main() {
Cuboid cuboid1 = new Cuboid(10, 6, 8);
cuboid1.DisplayResults();
Console.WriteLine();
Cuboid cuboid2 = new Cuboid(12, 7, 5);
cuboid2.DisplayResults();
}
}
The output of the above code is
Cuboid Dimensions: Length = 10, Breadth = 6, Height = 8 Volume: 480 cubic units Perimeter: 96 units Surface Area: 376 square units Cuboid Dimensions: Length = 12, Breadth = 7, Height = 5 Volume: 420 cubic units Perimeter: 96 units Surface Area: 358 square units
Comparison
| Approach | Time Complexity | Space Complexity | Reusability |
|---|---|---|---|
| Direct Formula | O(1) | O(1) | Low |
| Separate Functions | O(1) | O(1) | Medium |
| Object-Oriented | O(1) | O(1) | High |
Conclusion
Calculating the volume, perimeter, and surface area of a cuboid in C# can be implemented using direct formulas, separate functions, or object-oriented approaches. All methods have constant time and space complexity, with the object-oriented approach providing the best code organization and reusability for handling multiple cuboids.
