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

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

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

Introduction

A cylinder is a three-dimensional geometric shape with two parallel circular bases and a curved surface connecting them. Cylinders are frequently encountered in various real-world applications, such as pipes, tanks, and cans.

Calculating the surface area and volume of a cylinder is essential in fields like engineering, manufacturing, and design. In this article, we will write a C++ program to compute these properties, explaining the formulas, implementation, and output step by step.


Formula

The calculations for the surface area and volume of a cylinder depend on its radius (r) and height (h). The formulas are as follows:

  • Total Surface Area (TSA): 2Ï€r² + 2Ï€rh, where:
    • 2Ï€r² represents the area of the two circular bases.
    • 2Ï€rh represents the area of the curved surface.
  • Volume: Ï€r²h, which represents the space enclosed within the cylinder.

These formulas are derived from basic principles of geometry involving circles and three-dimensional shapes.


Code

Below is the C++ program that calculates the surface area and volume of a cylinder using user-provided values for radius and height:

#include <iostream>
#include <cmath>

using namespace std;

int main() {
    const double PI = 3.141592653589793;
    double radius, height, surfaceArea, volume;

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

    // Calculate surface area and volume
    surfaceArea = 2 * PI * radius * radius + 2 * PI * radius * height;
    volume = PI * radius * radius * height;

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

    return 0;
}
    

Output

When the program is executed, the user will be prompted to enter the radius and height of the cylinder. The program will then calculate and display the surface area and volume. Here is an example of the output:

Enter the radius of the cylinder: 3
Enter the height of the cylinder: 5
Surface Area of the cylinder: 150.796 square units
Volume of the cylinder: 141.372 cubic units
    

In this example:

  • Surface Area = 2Ï€(3²) + 2Ï€(3)(5) = 150.796 square units.
  • Volume = Ï€(3²)(5) = 141.372 cubic units.


Explanation

Let's understand the program's structure and functionality in detail:

  • Header Files:
    • <iostream> is used for input and output operations with cin and cout.
    • <cmath> is included to utilize mathematical functions and constants, such as PI.
  • Input:
    • The user is prompted to enter the radius and height of the cylinder.
    • The values are stored in variables radius and height, respectively.
  • Calculations:
    • The surface area is computed using the formula 2Ï€r² + 2Ï€rh, which includes both the base and lateral areas.
    • The volume is calculated as Ï€r²h, representing the space occupied by the cylinder.
  • Output:
    • The results are displayed using cout with appropriate labels for clarity.
    • Units (square and cubic) are included to specify the type of measurement.

This program demonstrates the application of geometry formulas and user-defined constants like PI in C++.


Conclusion

Calculating the surface area and volume of a cylinder is a fundamental geometric operation, and implementing it in C++ provides an excellent exercise in programming and mathematics. This program demonstrates how to:

  • Prompt the user for input values and process them using mathematical formulas.
  • Utilize constants like PI to improve readability and accuracy in calculations.
  • Display results clearly, with proper labels and units.