C++ Program to Find the Surface Area and Volume of a Rectangular Prism

C++ Program to Find the Surface Area and Volume of a Rectangular Prism

C++ Program to Find the Surface Area and Volume of a Rectangular Prism

Introduction

A rectangular prism is a three-dimensional shape with six faces, all of which are rectangles. It is also commonly referred to as a cuboid. Understanding how to calculate the surface area and volume of a rectangular prism is important for various real-world applications, such as in construction, packaging, and material estimation.

In this article, we will explore the concepts of surface area and volume for a rectangular prism. We will provide mathematical formulas, a C++ program to perform the calculations, and detailed explanations of the program's logic.


Formula

The calculations for the surface area and volume of a rectangular prism depend on its dimensions: length (l), width (w), and height (h). The formulas are:

  • Surface Area: The surface area of a rectangular prism is given by: 2 × (lw + lh + wh).
  • Volume: The volume of a rectangular prism is given by: l × w × h.

These formulas are derived from the properties of a rectangular prism. The surface area is the sum of the areas of all six rectangular faces, while the volume is the product of the three dimensions.


Code

Below is the C++ program that calculates the surface area and volume of a rectangular prism based on user input for length, width, and height:

#include <iostream>

using namespace std;

int main() {
    double length, width, height, surfaceArea, volume;

    // Input dimensions of the rectangular prism
    cout << "Enter the length of the rectangular prism: ";
    cin >> length;
    cout << "Enter the width of the rectangular prism: ";
    cin >> width;
    cout << "Enter the height of the rectangular prism: ";
    cin >> height;

    // Calculate surface area and volume
    surfaceArea = 2 * (length * width + length * height + width * height);
    volume = length * width * height;

    // Output results
    cout << "Surface Area of the rectangular prism: " << surfaceArea << " square units" << endl;
    cout << "Volume of the rectangular prism: " << volume << " cubic units" << endl;

    return 0;
}
    

Output

When the program is executed, the user is prompted to enter the length, width, and height of the rectangular prism. The program then calculates and displays the surface area and volume. Here’s an example of the output:

Enter the length of the rectangular prism: 5
Enter the width of the rectangular prism: 3
Enter the height of the rectangular prism: 2
Surface Area of the rectangular prism: 62 square units
Volume of the rectangular prism: 30 cubic units
    

In this example, the program calculates:

  • Surface Area = 2 × (5×3 + 5×2 + 3×2) = 62 square units.
  • Volume = 5 × 3 × 2 = 30 cubic units.


Explanation

Let’s break down the program step by step to understand its functionality:

  • Header File: The program includes <iostream>, which allows the use of input (cin) and output (cout) operations.
  • Input:
    • The user is prompted to enter the length, width, and height of the rectangular prism.
    • The input values are stored in the variables length, width, and height.
  • Calculations:
    • The surface area is calculated using the formula 2 × (lw + lh + wh), which sums the areas of all six faces of the rectangular prism.
    • The volume is calculated using the formula l × w × h, which gives the total space enclosed by the prism.
  • Output:
    • The calculated surface area and volume are displayed using cout.
    • Labels are added to clarify what each value represents.

This program demonstrates how to use basic arithmetic operations in C++ to perform geometric calculations. It also highlights the importance of user interaction through input and output functions.


Conclusion

The program to calculate the surface area and volume of a rectangular prism in C++ is a simple yet effective demonstration of basic programming concepts. By writing and running this program, you have learned how to:

  • Prompt the user for multiple inputs using cin.
  • Apply mathematical formulas using arithmetic operations.
  • Display results in a user-friendly format with cout.