C++ Program to Find Surface Area of a Rectangular Prism

C++ Program to Find Surface Area of a Rectangular Prism

A rectangular prism is a common 3D shape, also called a cuboid, with length, width, and height as its dimensions. Calculating its surface area is useful in real-world applications like packaging, construction, and 3D modeling. Learning how to implement this in C++ helps beginners understand arithmetic operations, input/output, functions, and arrays, making it a practical exercise for improving coding skills.

Pluralsight Logo
Accelerate your tech career
with hands-on learning.
Whether you're a tech newbie or a total pro,
get the skills and confidence to land your next move.
Start 10-Day Free Trial

Program 1: Surface Area Using User Input

This program calculates the surface area of a rectangular prism by taking length, width, and height from the user. It is a beginner-friendly way to practice reading inputs and applying formulas.

#include <iostream>

using namespace std;

int main() {

    double length, width, height, surfaceArea;

    cout << "Enter length, width, and height of the prism: "<< endl;
    cin >> length >> width >> height;

    surfaceArea = 2 * (length * width + width * height + height * length);
    cout << "The surface area of the rectangular prism is: " << surfaceArea << endl;

    return 0;

}

The formula 2 * (length*width + width*height + height*length) calculates the sum of the areas of all six faces. Beginners can clearly see how the formula translates into C++ code and how arithmetic operations are used.

Program 2: Surface Area Using Predefined Values

Sometimes you want to calculate quickly without entering input each time. This program demonstrates using predefined values for length, width, and height.

#include <iostream>

using namespace std;

int main() {

    double length = 5.0, width = 3.0, height = 4.0;
    double surfaceArea = 2 * (length * width + width * height + height * length);

    cout << "For a rectangular prism with L=" << length 
         << ", W=" << width << ", H=" << height 
         << ", the surface area is: " << surfaceArea << endl;

    return 0;

}

Using predefined values is useful for quick testing and understanding how different dimensions affect the surface area. Beginners can experiment by changing the values and observing results instantly.

Program 3: Surface Area Using a Function

Functions make your code cleaner and reusable. This program demonstrates calculating the surface area with a function.

#include <iostream>

using namespace std;

double calculateSurfaceArea(double length, double width, double height) {
    return 2 * (length * width + width * height + height * length);
}

int main() {

    double length, width, height;

    cout << "Enter length, width, and height of the prism: " << endl;
    cin >> length >> width >> height;

    double surfaceArea = calculateSurfaceArea(length, width, height);

    cout << "The surface area of the rectangular prism is: " << surfaceArea << endl;

    return 0;

}

By encapsulating the calculation in a function, the main program becomes shorter and easier to read. Beginners also learn the concept of modular programming and code reuse.

Program 4: Surface Area for Multiple Prisms Using Arrays

When you have multiple rectangular prisms, arrays help you calculate their surface areas efficiently. This program demonstrates the concept.

#include <iostream>

using namespace std;

int main() {

    double lengths[] {2.0, 4.0, 5.0};
    double widths[] {3.0, 2.5, 4.0};
    double heights[] {1.5, 3.0, 6.0};
    int n = 3;

    for(int i = 0; i < n; i++) {

        double surfaceArea = 2 * (lengths[i] * widths[i] + widths[i] * heights[i] + heights[i] * lengths[i]);

        cout << "Prism " << i+1 << " -> Surface Area: " << surfaceArea << endl;

    }

    return 0;

}

Using arrays and loops, beginners can handle multiple datasets without repeating code. This teaches iteration, array handling, and applying formulas across datasets.

Frequently Asked Questions (FAQ)

Q1: Can integers be used instead of doubles?
Yes, but doubles allow for fractional dimensions and more precise calculations.

Q2: Why use a function for surface area calculation?
Functions make the code cleaner, reusable, and easier to maintain.

Q3: Can the program handle negative or zero dimensions?
No, dimensions should be positive values for a valid rectangular prism.

Q4: How can I calculate surface area for multiple prisms?
Using arrays or vectors along with loops allows calculating surface areas efficiently.

Conclusion

Calculating the surface area of a rectangular prism in C++ is a practical exercise that reinforces arithmetic operations, input/output handling, functions, and arrays. Beginners can start with simple user input programs and gradually explore predefined values, functions, and multiple datasets using arrays. Practicing these examples strengthens both programming skills and understanding of real-world geometric calculations.

Additional & References

To continue learning, beginners should practice using functions, arrays, and loops for various 3D shapes such as cubes, cuboids, and pyramids. Exploring different approaches helps in connecting math concepts with programming logic.

Scroll to Top