C++ Program to Make a Simple Calculator to Add, Subtract, Multiply or Divide Using switch...case

C++ProgrammingServer Side Programming

Let us see a program to create a simple calculator in C++, with Add, Subtract, Multiply and Divide operations.

Example

 Live Demo

#include <iostream>
using namespace std;
void calculator(int a, int b, char op) {
   switch (op) {
      case '+': {
         cout<<"Sum of "<<a<<" and "<<b<<" is "<<a+b<<endl;
         break;
      }
      case '-': {
         cout<<"Difference of "<<a<<" and "<<b<<" is "<<a-b<<endl;
         break;
      }
      case '*': {
         cout<<"Product of "<<a<<" and "<<b<<" is "<<a*b<<endl;
         break;
      }
      case '/': {
         cout<<"Division of "<<a<<" and "<<b<<" is "<<a/b<<endl;
         break;
      }
      default:
      cout<<"Invalid Input"<<endl;
   }
}
int main() {
   calculator(5,4,'+');
   calculator(10,3,'-');
   calculator(3,2,'*');
   calculator(20,5,'/');
   calculator(5,2,'?');
   return 0;
}

Output

Sum of 5 and 4 is 9
Difference of 10 and 3 is 7
Product of 3 and 2 is 6
Division of 20 and 5 is 4
Invalid Input

In the above program, a function calculator is used to add, subtract, multiply and divide two numbers. This is done using a switch case statement. The function takes 3 parameters i.e. two numbers on which the operation is to be performed and what operation is to be performed. This is shown as follows −

void calculator(int a, int b, char op)

There are 4 cases in the switch case statement and one default case. The first case is used when addition is to be performed. The two numbers are added and their sum is displayed. This is shown using the following code snippet.

case '+': {
   cout<<"Sum of "<<a<<" and "<<b<<" is "<<a+b<<endl;
   break;
}

The second case is used when subtraction is to be performed. The two numbers are subtracted and their difference is displayed. This is shown using the following code snippet.

case '-': {
   cout<<"Difference of "<<a<<" and "<<b<<" is "<<a-b<<endl;
   break;
}

The third case is used when multiplication is to be performed. The two numbers are multiplied and their product is displayed. This is shown using the following code snippet.

case '*': {
   cout<<"Product of "<<a<<" and "<<b<<" is "<<a*b<<endl;
   break;
}

The fourth case is used when division is to be performed. The two numbers are divided and their division is displayed. This is shown using the following code snippet.

case '/': {
cout<<"Division of "<<a<<" and "<<b<<" is "<<a/b<<endl;
break;
}

The default case is used for invalid operator provided.This is shown using the following code snippet.

default: cout<<"Invalid Input"<<endl;

The function calculator() is called from main() for different operations and using different operands. This is demonstrated by the following code snippet.

calculator(5,4,'+');
calculator(10,3,'-');
calculator(3,2,'*');
calculator(20,5,'/');
calculator(5,2,'?');
raja
Published on 27-Sep-2018 10:55:10
Advertisements