C/C++ program to make a simple calculator?


A simple calculator is a calculator that performs some basic operations like ‘+’ , ‘-’ , ‘*’ , ‘/’. A calculator does basic operation in a fast way. We will use switch statement to make a calculator.

Sample

Operator − ‘+’ => 34 + 324 = 358
Operator − ‘-’ => 3874 - 324 = 3550
Operator − ‘*’ => 76 * 24 = 1824
Operator − ‘/’ => 645/5 = 129

Example Code

#include <iostream<
using namespace std;
int main() {
   char op = '+';
   float a = 63, b= 7;
   // cout << "Enter operator either + or - or * or /: ";
   // cin >> op;
   cout<<"The operator is "<<op<<endl;
   // cout << "Enter two operands: ";
   // cin>> a >> b;
   cout<<"a = "<<a<<" b = "<<b<<endl;
   switch(op) {
      case '+':
         cout <<"Sum of"<<a<<"and"<<b<<"is"<< a+b;
      break;
      case '-':
         cout <<"Difference of"<<a<<"and"<<b<<"is"<<a-b;
      break;
      case '*':
         cout <<"Product of"<<a<<"and"<<b<<"is"<<a*b;
      break;
      case '/':
         cout <<"Division of"<<a<<"and"<<b<<"is"<<a/b;
      break;
      default:
         // If the operator is other than +, -, * or /, error message is shown
         cout << "Error! operator is not correct";
      break;
   }
   return 0;
}

Output

The operator is +
a = 63 b = 7
Sum of63 and 7 is 70

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 30-Jul-2019

712 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements