A menu driven program to get 2 numbers entered by the user and then perform any of the basic mathematical operations (i.e. add, subtract, multiply and divide) according to the choice of the user.

 

#include<iostream.h>
#include<conio.h>

void main()
{
double a,b,c;
int ch;

do
 {
  clrscr();

  cout<<"1. Add."<<endl;
  cout<<"2. Substract."<<endl;
  cout<<"3. Multiply."<<endl;
  cout<<"4. Divide."<<endl;
  cout<<"5. Exit."<<endl;
  cout<<"Enter your choice: ";
  cin>>ch;

  switch(ch)
   {
    case 1: {
                cout<<"\nEnter value of first number: ";
                cin>>a;
                cout<<"Enter value of second number: ";
                cin>>b;
                c=a+b;
                cout<<"\nSum: "<<c;
                getch();
                break;
               }
    case 2: {
                cout<<"\nEnter value of first number: ";
                cin>>a;
                cout<<"Enter value of second number: ";
                cin>>b;
                c=a-b;
                cout<<"\nDifference: "<<c;
                getch();
                break;
               }    
    case 3: {
                cout<<"\nEnter value of first number: ";
                cin>>a;
                cout<<"Enter value of second number: ";
                cin>>b;
                c=a*b;
                cout<<"\nProduct: "<<c;
                getch();
                break;
               }
    case 4: {
                cout<<"\nEnter value of first number: ";
                cin>>a;
                cout<<"Enter value of second number: ";
                cin>>b;
                c=a/b;
                cout<<"\nQuotient: "<<c;
                  getch();
                break;
               }
    case 5: {
                break;
               }
    default: {
                 cout<<"\nWrong choice entered.\n";
                 getch();
                 break;
                }
   }
  }
 while(ch!=5);
}

 

 

< Back