Write a program in C++ to calculate the volume of a cylinder.
What is a cylinder?
There are many types of cylinders. This page describes the simplest shape, where a cylinder looks like a tube or can of soup, with two equally sized parallel circles at each end.
Cylinder Terminology
To calculate the volume of a cylinder, first need to understand some terms.
Radius - The radius is the distance from the
center of the end circle to the end.
Pi - Pi is a special number used in circles. We use a shortened version of Pi = 3.14. We also use the symbol π to refer to the number pi in the equation.
Height - Cylinder height or length.
Volume of a Cylinder
There is a special formula for finding the volume of a cylinder. Volume indicates the amount of space occupied by the interior of the cylinder. Answers to questions about volume are always in cubic units.
Volume = πr2h
This is the same as 3.14 x radius x radius x height.
Sample
Solution:
C++ Code :
#include <iostream>
using namespace std;
int main()
{
int rad1,hgt;
float volcy;
cout << "\n\n Calculate the
volume of a cylinder :\n";
cout << "-----------------------------------------\n";
cout<<" Input the radius of the cylinder :
";
cin>>rad1;
cout<<" Input the height of the cylinder : ";
cin>>hgt;
volcy=(3.14*rad1*rad1*hgt);
cout<<" The volume of a cylinder is : "<< volcy << endl;
cout << endl;
return 0;
}
Sample
Output:
Calculate
the volume of a cylinder :
-----------------------------------------
Input the radius of the cylinder : 6
Input the height of the cylinder : 8
The volume of a cylinder is : 904.32

No comments:
Post a Comment