Monday, March 28, 2011

if...else & switch...case











#include <iostream.h>
main()
{
      //declare
      float b,h,r,w,area;
      float c,f,cel,fah;
      float pi=3.14;
      int selection;
      //out
      cout<<"Please choose you choice"<<endl;
      cout<<"1.Rectangular";
      cout<<"\n2.Triangle";
      cout<<"\n3.Circle";
      cout<<"\n4.Fahrenheit";
      cout<<"\n5.Celcius"<<endl;
      cout<<"Your Selection is:"<<endl;
      cin>>selection;
      switch (selection)
      {
         case 1: cout<<"Please input the height"<<endl;
                     cin>>h;
                     cout<<"Please input the width"<<endl;
                     cin>>w;
                     //process
                     area = w*h;
                     cout<<"Your selection is"<<selection;cout<<"and you are calculate Rectangular"<<endl;
                     cout<<"The answer is:"<<area;
                     break;
         case 2: cout<<"Please input the height"<<endl;
                     cin>>h;
                     cout<<"Please input the base"<<endl;
                     cin>>b;
                     //process
                     area = 0.5*b*h;
                     cout<<"Your selection is"<<selection;cout<<"and you are calculate Triangle"<<endl;
                     cout<<"The answer is:"<<area;
                     break;
         case 3: cout<<"Please input the radius"<<endl;
                     cin>>r;
                     //process
                     area = pi*r*r;
                     cout<<"Your selection is"<<selection;cout<<"and you are calculate Circle"<<endl;
                     cout<<"The answer is:"<<area;
                     break;
         case 4: cout<<"Please insert the celcius"<<endl;
                     cin>>c;
                     //process
                     fah= 9/5*(c+32);
                     cout<<"Your selection is"<<selection;cout<<"and you are calculate Fahrenheit"<<endl;
                     cout<<"The answer is:"<<fah;
                     break;
         case 5: cout<<"Please insert the fahrenheit"<<endl;
                     cin>>f;
                     //process
                     cel = 5/9*(f+32);
                     cout<<"Your selection is"<<selection;cout<<"and you are calculate Celcius"<<endl;
                     cout<<"The answer is:"<<cel;
                     break;
         default :cout<<"Invalid Selection";
          return 0;
             }
}

No comments:

Post a Comment