Monday, March 12, 2012

C++: Compute for the Volume (Rectangular, Cone, Cylinder etc)



#include <iostream>
using namespace std;
 
int rectangular (int l, int w, int h)
{
  int rect_vol;
  rect_vol=l*w*h;
  return (rect_vol);
}
int prism (int a, int b)
{
  int pri_vol;
  pri_vol=a*b;
  return (pri_vol);
}
int cylinder (int a, int b)
{
  int cyl_vol,pi=3.1416;
  cyl_vol=((pi)*(a*a)*b);
  return (cyl_vol);
}
double pyramid (double a, double b)
{
  double pyr_vol;
  pyr_vol=(a*b)/3;
  return (pyr_vol);
}
 int cones (int a, int b)
{
  int con_vol,pi=3.1416;
  con_vol=((pi)*((a*a)*b))/3;
  return (con_vol);
}
double sphere (int a)
{
  int sph_vol,pi=3.1416;
  sph_vol=(4/3)*((pi)*(a*a*a));
  return (sph_vol);
}

int main ()
{
 
  char choice;
  double l,w,h,r,b,pi=3.1416;
  double rec,pri,cyl,pyr,con,sph;
  cout<<"Choose a polygon: \na-Rectangular \nb-Prism\nc-Cylinder\nd-Pyramid\ne-Cones\nf-Sphere"<<endl;
  cin>>choice;
  switch (choice)
  {
  case 'a':
cout<<"Enter length";
cin>>l;
cout<<"Enter width";
cin>>w;
cout<<"Enter height";
cin>>h;
rec = rectangular (l,w,h);
cout << "The volume of the rectangle is "<<rec;
break;
case 'b':
cout<<"Enter base";
cin>>b;
cout<<"Enter height";
cin>>h;
pri = prism (b,h);
cout << "The volume of the prism is "<<pri;
break;
case 'c':
cout<<"Enter radius";
cin>>r;
cout<<"Enter height";
cin>>h;
cyl = cylinder (r,h);
cout << "The volume of the cylinder is "<<cyl;
break;
case 'd':
cout<<"Enter base";
cin>>b;
cout<<"Enter height";
cin>>h;
pyr = pyramid (b,h);
cout << "The volume of the pyramid is "<<pyr;
break;
case 'e':
cout<<"Enter radius";
cin>>r;
cout<<"Enter height";
cin>>h;
con = cones (r,h);
cout << "The volume of the cones is "<<con;
break;
case 'f':
cout<<"Enter radius";
cin>>r;
sph = sphere (r);
cout << "The volume of the sphere is "<<sph;
break;
default: 
cout<<"Out of coverage"<<endl;
}
  return 0;
}

No comments: