A Simple Calculator in C++

0
1076
Comparing two Numbers in C++
Calculation of Summation in C++

If we want to create a simple calculator in C++ which is able to operate simple arithmetic operations  like addition, subtraction, multiplication and division. again here is an example of applications of arithmetic and conditional operators in C++. Here is  our simple Calculator’s C++ code.

 

 

lets look at it.

    //Creating a simple calculator in C++
    #include <iostream>
    using namespace std;
    int main()
    {
        float num1,num2;        //numbers "num1"and"num2"
        char op;
     
        cout<<"enter the statement:n";
        //asking the user for statement
        cin>>num1>>op>>num2;
     
        if (op== '+')           //condition for addition
            cout<<num1<<"+"<<num2<<"="      <<num1+num2<<endl;
     
        if (op=='-')           //condition for subtraction
            cout<<num1<<"-"<<num2<<"="<<num1-num2<<endl;
     
        if (op=='*')//condition for multiplication
            cout<<num1<<"*"<<num2<<"="  <<num1*num2<<endl;
     
        if (op=='/')   //condition for division
            cout<<num1<<"/"<<num2<<"="<<num1/num2<<endl;
        return 0;
    }

First of all we took two variables of datatype  “float” and a variable of type “char”. then we asked the user to enter the the statement he want to calculate.e.g 3+4, after this the program searched for the conditional structure true for the variable “op”, if we want to add the two numbers, the first conditional structure is called,similarly second condition for subtraction and so on.and at the end we got our result just like a calculator. And that was our simple arithmetic calculator in C++.

Comparing two Numbers in C++
Calculation of Summation in C++

Leave a Reply