C++ Program to Find Surface Area of a Cylinder

C++ Program to Find Surface Area of a Cylinder

A cylinder is one of the most common 3D shapes, appearing in objects like cans, pipes, and tanks. Calculating its surface area is useful in engineering, construction, and manufacturing. Learning how to compute the surface area in C++ gives beginners practice with mathematical formulas, input/output handling, and basic programming constructs.

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 cylinder by asking the user for the radius and height. It is a simple way for beginners to learn how to read input and apply the surface area formula.

#include <iostream>
#include <cmath>

using namespace std;

int main() {

    double radius, height, surfaceArea;

    cout << "Enter the radius of the cylinder: " << endl;
    cin >> radius;

    cout << "Enter the height of the cylinder: " << endl;
    cin >> height;

    surfaceArea = 2 * M_PI * radius * (radius + height);
    cout << "The surface area of the cylinder is: " << surfaceArea << endl;

    return 0;

}

The formula 2 * π * r * (r + h) adds the areas of the two circular bases and the curved surface. Beginners can clearly see how mathematical formulas translate into C++ expressions.

Program 2: Surface Area Using Predefined Values

Sometimes you want to test calculations quickly without user input. This program uses predefined radius and height values for easy testing.

#include <iostream>
#include <cmath>

using namespace std;

int main() {

    double radius = 5.0, height = 10.0;
    double surfaceArea = 2 * M_PI * radius * (radius + height);

    cout << "For a cylinder with radius=" << radius 
         << " and height=" << height 
         << ", the surface area is: " << surfaceArea << endl;

    return 0;

}

Using predefined values helps beginners experiment with different dimensions and instantly see the result, making it easier to understand how radius and height affect the surface area.

Program 3: Surface Area Using a Function

Encapsulating the calculation inside a function makes the code cleaner and reusable. This program demonstrates that approach.

#include <iostream>
#include <cmath>

using namespace std;

double calculateSurfaceArea(double radius, double height) {
    return 2 * M_PI * radius * (radius + height);
}

int main() {

    double radius, height;

    cout << "Enter radius and height of the cylinder: " << endl;
    cin >> radius >> height;

    double surfaceArea = calculateSurfaceArea(radius, height);

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

    return 0;

}

By using a function, beginners can separate logic from input/output. This also demonstrates modular programming, making the code easier to read and maintain.

Program 4: Surface Area for Multiple Cylinders Using Arrays

When you have multiple cylinders, arrays and loops allow calculating surface areas efficiently. This program demonstrates handling multiple datasets.

#include <iostream>
#include <cmath>

using namespace std;

int main() {

    double radii[] {3.0, 5.0, 7.0};
    double heights[] {10.0, 12.0, 15.0};
    int n = 3;

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

        double surfaceArea = 2 * M_PI * radii[i] * (radii[i] + heights[i]);

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

    }

    return 0;

}

Arrays and loops allow beginners to handle multiple inputs without duplicating code. This demonstrates iteration and array manipulation, key skills for practical programming.

Frequently Asked Questions (FAQ)

Q1: Can I use integers instead of doubles for radius and height?
Yes, but doubles allow for fractional values and more precise surface area calculations.

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

Q3: Can the program handle multiple cylinders efficiently?
Yes, by using arrays and loops, you can calculate surface areas for many cylinders in one run.

Q4: What is π in the program?
We define π (pi) as a constant because C++ doesn’t provide a default value. You can also use M_PI from <cmath> with some compilers.

Conclusion

Calculating the surface area of a cylinder in C++ is a practical exercise that teaches formulas, input/output, functions, arrays, and loops. Beginners can start with simple user input programs and gradually explore predefined values, modular functions, and multiple datasets using arrays. Practicing these examples strengthens programming skills and builds confidence in solving real-world geometric problems.

Additional & References

To continue learning, beginners should practice using functions, arrays, and loops for different 3D shapes such as cones, spheres, and cylinders. Experimenting with multiple methods helps improve both coding skills and understanding of mathematical concepts.

Scroll to Top