Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
Resource icon

My basic calculator code for C++

C++:
//
// Created by malcolm on 2020-07-31.
//
// Advance Calculator

#include <iostream>
using namespace std;

int main(){
    int firstNum {0};
    int secNum {0};
    int maths {0};

    // Asking for two numbers
    cout << "Enter your first number\n";
    cin >> firstNum;
    cout << "Enter your second number\n";
    cin >> secNum;

    // Asking for operator
    cout << "What operator? (1: Addition 2: Subtraction 3: Multiplication 4: Divide 5: modulo)\n";
    cin >> maths;

    if(maths==1) {
        cout << firstNum + secNum << endl;
        return main();
    } else if (maths==2){
        cout << firstNum - secNum << endl;
        return main();
    } else if (maths==3){
        cout << firstNum * secNum << endl;
        return main();
    } else if (maths==4){
        cout << firstNum / secNum << endl;
        return main();
    } else if (maths==5){
        cout << firstNum % secNum << endl;
        return main();
    } else {
        cout << "Unknown Operator \n";
    }
    return main();
}
Back
Top Bottom