Find Force and Distance Using Switch Case Statement

By | February 1, 2013
Force-and-DistanceThe Switch case statement is a program which can be used as if/else statement. When sometime in a program we use repeatedly if/else statement so we can short replace programming code of switch/case statement. In a simple language we can say that switch case statement usually used to replace multiple use of if/else statement.A sample example is being given below.

Example

 Switch (variable)
{
Case possible-value:
//code to execute
Break;
Default:
//code to execute
}

 

Question:

  • Write a menu-driven program that calculates:
  1. Force F=m*a
  2. Distance S=Vi*t+0.5*a*t*t
  3. Find x, x=(a+b)(a-b)

Source Code

#include
using namespace std;
int main()
{
            int a,b,m,f;
            cout<<”Choose an option \n1 – Calculate Force \n2 – Calculate Distance \n3 – Calculate x \n4 – Exit \n”;
            cin>>a;
            switch (a)
            {
            case 1:
                        cout<<”Enter mass value”;
                        cin>>m;
                        cout<<”Enter Acceleration”;
                        cin>>b;
                        f=m*b;
                        cout<<” Force value is”<<f<<endl;
                        break;
            case 2:
                        int i,t,a,s;
                        cout<<”Enter Initial Velocity”;
                        cin>>i;
                        cout<<”Enter time”;
                        cin>>t;
                        cout<<”Enter Acceleration”;
                        cin>>a;
                        s=i*t+0.5*a*t*t;
                        cout<<”Distance is”<<s<<endl;
                        break;
            case 3:
                        int x,o,b;
                        cout<<”Enter value a”;
                        cin>>o;
                        cout<<”Enter value b”;
                        cin>>b;
                        x=(o+b)*(o-b);
                        cout<<”X value is”<<x<<endl;
                        break;
            case 4:
                        cout<<”Exit\n”;
                        break;

}

}
 

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS