C++ Program to Find Volume of a Cylinder

C++ Program to Find Volume of a Cylinder

Finding the volume of a cylinder is a practical application of geometry and programming. Cylinders appear everywhere in real life, from water tanks to pipes, and calculating their volume helps determine how much space they occupy. Learning how to implement this calculation in C++ is a great exercise for beginners, as it involves mathematical formulas, user input, and basic programming structures, helping to build a strong foundation in coding and problem-solving.

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: Calculate Volume of a Cylinder Using User Input

This program asks the user to enter the radius and height of a cylinder and calculates its volume using the formula: Volume = π × radius² × height.

#include <iostream>
#include <cmath>

using namespace std;

int main() {

    double radius, height, volume;

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

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

    volume = M_PI * radius * radius * height;
    cout << "Volume of the cylinder is: " << volume << endl;

    return 0;

}

The program works by taking the radius and height from the user, calculating the area of the circular base (π × radius²), multiplying by the height, and outputting the volume. Beginners can see how math formulas translate into C++ code and understand the flow of taking input, performing calculations, and displaying results.

Program 2: Calculate Volume Using Predefined Values

Sometimes, predefined values are useful for testing or demonstration purposes. This program calculates the volume of a cylinder using fixed radius and height values.

#include <iostream>
#include <cmath>

using namespace std;

int main() {

    double radius = 5.0;
    double height = 10.0;
    double volume = M_PI * radius * radius * height;

    cout << "Volume of the cylinder is: " << volume << endl;

    return 0;

}

By using predefined values, beginners can quickly verify the formula and understand the calculation without needing user input. This approach is excellent for learning and debugging.

Program 3: Calculate Volume for Multiple Cylinders Using Arrays

If you want to calculate volumes for multiple cylinders, arrays can be used to store different radius and height values. This program demonstrates that approach.

#include <iostream>
#include <cmath>

using namespace std;

int main() {

    double radii[] {2, 3, 4};
    double heights[] {5, 6, 7};
    int n = 3;

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

        double volume = M_PI * radii[i] * radii[i] * heights[i];
        cout << "Cylinder " << i+1 << " Volume: " << volume << endl;

    }

    return 0;

}

Using arrays and loops allows beginners to perform repetitive calculations efficiently. This method introduces the concept of handling multiple datasets and applying formulas in a scalable way.

Program 4: Calculate Volume Using a Function

Encapsulating the volume calculation in a function makes the program modular and reusable. Beginners can see how functions help organize code.

#include <iostream>
#include <cmath>

using namespace std;

double cylinderVolume(double radius, double height) {
    return M_PI * radius * radius * height;
}

int main() {

    double radius, height;

    cout << "Enter radius: " << endl;
    cin >> radius;

    cout << "Enter height: " << endl;
    cin >> height;

    double volume = cylinderVolume(radius, height);
    cout << "Volume of the cylinder is: " << volume << endl;

    return 0;

}

Functions allow the calculation to be reused with different inputs, making the code cleaner and easier to maintain. Beginners also learn about parameters, return values, and modular design, which are essential programming concepts.

Frequently Asked Questions (FAQ)

Q1: Can the program handle negative values?
Technically yes, but negative radius or height does not make sense in real-world scenarios. Always use positive numbers.

Q2: Why use #define PI instead of writing 3.14 directly?
Using #define PI improves accuracy and makes the code easier to read and maintain.

Q3: Can this program calculate volume in different units?
Yes, by ensuring radius and height are in the desired units (e.g., meters or centimeters), the volume will be in cubic units of the same system.

Q4: Can I calculate the surface area along with volume?
Yes, you can add another function for surface area using the formula: Surface Area = 2 × π × radius × (radius + height).

Conclusion

Calculating the volume of a cylinder in C++ is an excellent way for beginners to practice math operations, loops, arrays, functions, and user input/output. By trying user input, predefined values, arrays for multiple cylinders, and functions, learners strengthen their understanding of both C++ syntax and problem-solving. This skill also connects directly to real-life applications like measuring liquid capacity, storage containers, or cylindrical objects in construction and engineering.

Additional & References

Beginners should experiment with different radius and height values, arrays, and functions to improve coding skills and understanding of formulas. Practicing these variations helps build confidence and prepares learners for more complex projects.

Scroll to Top