Wednesday, 20 May 2015

How To Create a Calculator Using Switch Statement

Create a calculator in C++
This is a simple c++ program to show how switch statement is used and how can we make a calculator using switch. This simple calculator can work with only four operations i.e addition,subtraction,multiplication and division.
select any one operation and enter the two values to calculate.



#include<iostream>
#include<math.h>

using namespace std;
int main ()
{
    int choice;
    float a,b,c;

    cout<<"\t\t\t**SELECT CHOICE** \n '1' for addition\n '2' for subtraction\n '3' for multiplication\n '4' for division" <<endl<<endl<<endl;
    cout<<"Enter your Choice : ";
    cin>>choice;
    cout<<"\n\nEnter first number : ";
    cin>>a;
    cout<<"Enter second number :";
    cin>>b;
    cout<<endl<<endl;
  switch (choice)

   {
            case 1:
                 c=a+b;
                 cout<<"Addition is : "<<c <<endl;
                 break;
            case 2:
                 c=a-b;
                 cout<<"Subtraction is : "<<c <<endl;
                 break;
            case 3:
                 c=a*b;
                 cout<<"Multiplication is : "<<c <<endl;
                 break;
            case 4:
                 if(b==0)
                 cout<<"Undefined";
                 else
                 {
                 c=a/b;
                 cout<<"Division is : "<<c <<endl<<endl;
                 }
                 break;
                 }
            system("pause");
            return 0;
      }

0 comments:

Post a Comment