Simple Calculator Program In C++

Simple Calculator Program In C++

Here's the code for simple calculator program in C++:

#include <iostream>

using namespace std;

class calculator
{
public:
    void add(){
        int a,b;
        cout << "Enter first number:";
        cin >> a;
        cout << "Enter second number:";
        cin >> b;
        cout << "addition is :" << a+b;
    }
    void sub(){
        int a,b;
        cout << "Enter first number:";
        cin >> a;
        cout << "Enter second number:";
        cin >> b;
        cout << "subtraction is :" << a-b;
    }
    void multi(){
        int a,b;
        cout << "Enter first number:";
        cin >> a;
        cout << "Enter second number:";
        cin >> b;
        cout << "multiplication is :" << a*b;
    }
    void divi(){
        int a,b;
        cout << "Enter first number:";
        cin >> a;
        cout << "Enter second number:";
        cin >> b;
        cout << "division is :" << a/b;
    }
};
int main()
{
    calculator calc;
    int get;
    for(int i=1;i>0;i++)
    {
        cout << "\n\nPress:\n\t1 : addition\n\t2 : subtraction\n\t3 : multiplication\n\t4 : division\n\t0 : quit\n";
        cin >> get;
        if(get==1){
            calc.add();
        } else if(get==2){
            calc.sub();
        } else if(get==3){
            calc.multi();
        } else if(get==4){
            calc.divi();
        } else{
            break;
        }
    }
    return 0;
}


Output:

SIMPLE CALCULATOR IN C++

Leave a Reply

Make sure you tick the "Notify Me" box below the comment form to be notified of follow up comments and replies.