Chapter: Switch Case Control Statement

INDEX

using namespace std;

int main()
{
    int day_num;

    cout<<endl<<"Enter day number of the week: ";
    cin>>day_num;

    switch(day_num)
    {
        case 1:
            cout<<endl<<"Selected day is SUNDAY."
            break;
        case 2:
            cout<<endl<<"Selected day is MONDAY."
            break;
        case 3:
            cout<<endl<<"Selected day is TUESDAY."
            break;
        case 4:
            cout<<endl<<"Selected day is WEDNESDAY."
            break;
        case 5:
            cout<<endl<<"Selected day is THURSDAY."
            break;
        case 6:
            cout<<endl<<"Selected day is FRIDAY."
            break;
        case 7:
            cout<<endl<<"Selected day is SATURDAY."
            break;
        default:
            cout<<endl<<"Error!!! Invalid Day-Number. Enter between 1 to 7."
           
    };
    return 0;
};

/*Similarly try the following programs:
    Accept Month-number and display Month-name.
    Accept colour-code and display colour-name (0-black,1-red,2-green,3-blue,4-white).
    Accept month-number and display the number of days in that month.
*/

TOP

/*Write a program to accept two numbers and an operand(+,-,x,/) then perform appropriate calculation as per the operator entered and display the corresponding output
Author: Pradeep Das Gupta

Compiler: GNU GCC Compiler*/
#include<iostream>

using namespace std;

int main()
{
    float x,y,z;
    char op;

    cout<<"\nEnter two numbers (X): ";
    cin>>x;
    cout<<"\n(Y): ";
    cin>>y;

    cout<<"\nEnter operator (+,-,x,/): ";
    cin>>op;

    switch(op)
    {
        case '+':
            z=x+y;
            cout<<"\n"<<x<<" + "<<y<<" = "<<z;
            break;
        case '-':
            z=x-y;
            cout<<"\n"<<x<<" - "<<y<<" = "<<z;
            break;
        case 'x':
            z=x*y;
            cout<<"\n"<<x<<" x "<<y<<" = "<<z;
            break;
        case '/':
            if(y==0)
            {
                cout<<"\nError!!! Division by Zero not allowed.";
                break;
            }
            z=x/y;
            cout<<"\n"<<x<<" / "<<y<<" = "<<z;
            break;
        default:
            cout<<"\nError!!! Invalid Operator.";
    }
    return 0;
}

TOP

/*A menu driven program:
Write a program to display a menu as follows;

1.Area of Triangle (using base and altitude)
2.Area of triangle (using 3 sides)
3.Area of equilateral triangle
4.Exit
Select your operation (1,2,3 or 4):

The program should take the user's choice and based upon his/her choice, it should perform the appropriate operation.

Author: Pradeep Das Gupta

Compiler: GNU GCC Compiler
*/
#include<iostream>
#include<math.h>
#include<stdio.h>
#include<stdlib.h>

using namespace std;

int main()
{
    float x,y,z,ar,temp;
    short int ch;

    do
    {
        system("cls");//clears the screen

        cout<<"\nMENU\n********************";
        cout<<"\n1.Area of Triangle (using base and altitude)"
            <<"\n2.Area of triangle (using 3 sides)"
            <<"\n3.Area of equilateral triangle"
            <<"\n4.Exit"
            <<"\nSelect your operation (1,2,3 or 4): ";
        cin>>ch;
       
        switch(ch)
        {
            case 1: //Area of Triangle (using base and altitude)
                cout<<"\nEnter Base: ";
                cin>>x;
                cout<<"\nEnter Corresponding Altitude: ";
                cin>>y;
                ar=0.5*x*y;
                cout<<"\nArea = "<<ar<<" sq. units";
                break;
            case 2: //Area of triangle (using 3 sides)
                cout<<"\nEnter Side1: ";
                cin>>x;
                cout<<"\nEnter Side2: ";
                cin>>y;
                cout<<"\nEnter Side3: ";
                cin>>z;
                temp=0.5*(x+y+z);//semi-perimeter
                ar=sqrt(temp*(temp-x)*(temp-y)*(temp-z));
                cout<<"\nArea = "<<ar<<" sq. units";
                break;
            case 3: //Area of equilateral triangle
                cout<<"\nEnter the side: ";
                cin>>x;
                ar=sqrt(3)*0.25*pow(x,2);
                cout<<"\nArea = "<<ar<<" sq. units";
                break;
            case 4: //Exit
                break;
            default: //For Invalid Choice
                cout<<"\nERROR!!! Invalid Choice Entered."
                    <<"\nChoose between 1 to 4.";
        }
        cout<<"\nProcess Paused...\nPress [ENTER] to continue.....";
        getc(stdin);
    }while(ch!=4);

    return 0;
}

TOP