RUN


Program to make basic calculator

This can be done by simply using the switch case where the cases will be the operators (+,-,*,/).

Algorithm

  1. Enter two operands on which we have to perform operation.
  2. Enter the operator.
  3. The switch statement searches if the operator matches any of the cases (+,-,*,/). If there is any other operator then error message will be displayed.
  4. Print the answer after operation is performed.

Code

// C++ program to create calculator

#include <iostream> 

using namespace std; 


// Main program 

int main() 

{ 

    char oper; 

    float a, b; 

     // It allow user to enter the operands 

    cout<<"Enter two operands: ";

    cin>> a >> b; 

    // It allows user to enter operator i.e. +, -, *, / 

    cout<<"Enter operator: ";

    cin>> oper; 

    // Switch statement begins 

    switch (oper) 

    { 

        // If operator is '+'  

        case '+': 

            cout << a + b; 

            break; 

        // If operator is '-'  

        case '-': 

            cout << a - b; 

            break; 

        // If operator is '*' 

        case '*': 

            cout << a * b; 

            break; 

        // If operator is '/' 

        case '/': 

            cout << a / b; 

            break; 

        // If any other operator display error message 

        default: 

            cout << "Error! Incorrect operator"; 

            break; 

    } 

    return 0;  

}

Report Error/ Suggestion

Related Posts:

[yuzo_views]



Interview Questions



CopyRight © 2020

CopyRight © 2020