Signup/Sign In
LAST UPDATED: DECEMBER 4, 2020

Program for Simple Calculator in C++

Following is the program for Simple Calculator using switch statements.

#include<iostream.h>

int main()
{
    char op;
    float num1, num2;
    cout << "Enter operator either + or - or * or /: ";
    cin >> op;
    cout << "\nEnter two operands: ";
    cin >> num1 >> num2;
    switch(op)
    {
        case '+':
            cout <<”\nResult is: ”<< num1+num2;
            break;

        case '-':
            cout <<”\nResult is: ”<< num1-num2;
            break;

        case '*':
            cout <<”\nResult is: ”<<num1*num2;
            break;

        case '/':
            cout <<”\nResult is: ”<<num1/num2;
            break;

        default:
            // If the operator is other than +, -, * or /, error message is shown
            cout<<"Error! operator is not correct";
            break;
    }
getch();
return 0 ;
}


Enter operator either + or - or * or / : -
Enter two operands: 3.4
8.4
Result is: -5.0



About the author:
Nikita Pandey is a talented author and expert in programming languages such as C, C++, and Java. Her writing is informative, engaging, and offers practical insights and tips for programmers at all levels.