SIZ-Tutorials || How to program a digital calculator by C++ Language || by @aqsaejaz

in Steem Infinity Zone3 years ago (edited)

20% to @siz-official

Assalam-o-Alaikum, Everyone!

Hope y'all are doing great. Today I've decided to share a simple tutorial with you guys that is programming a digital calculator through a programming language, C++.

The calculator that we're going to create here, will have 4 functionalities i.e addition, subtraction, multiplication and division. Its the easiest and simplest way to program a simple calculator. Hope you will enjoy :)

image.png

The working of this calculator will be like, the user have to select the operator and then he has to enter two numbers to perform the calculation and then will display the result.

Let's get started....

Step 1:

  • First of all, You can download dev C++ to compile your code or you can use an online interpreter. Well i use an online interpreter, I have provided the link below.

2.jpeg

source

  • In this code I've included the ios stream file and then i'll be using here the components which are defined inside the std namespace. and Now i the next line , I've written the main function which is the entry point of the program.
#include <iostream>
using namespace std;
int main() {
    return 0;
}

image.png

Step 2:

  • I'll declare the variables, the user will enter the operator and to store that operator, so we need a variable i.e character data type. And to store the numbers of the user , we'll declare two variables.

I've declared my variables as number 1 and number 2.

#include <iostream>
using namespace std;

int main() {
    char oper;
    double number1,number2;
     return 0;
}

Step 3:

  • We'll ask the user to enter the operator and we will display the choice also. We'll store that in the oper and to read the operator we'll use cin.
  cout<<"Enter operation(+,-,/,*): ";
    cin >> oper;
  • Now we'll ask the user to enter the numbers and then store those numbers in the variables number1 and number2.
cout<<"Enter two numbers one by one: ";
    cin >> num1 >> num2;

image.png

Step 4:

To display the result, we'll use the switch statement and the expression will be operator that the user have entered.
now inside this switch statement, we'll write the code for every operation.

  • Addition operation will be our first case, we'll first display the value of number 1 variable then plus sign and then the value of number2 variable.
 switch ( op ){
        case '+':
             cout<< num1 << '+' << num2 << '=' << (num1 + num2);
            break;
}

image.png

Step 5:

Repeat the same for multiplication and subtraction, like this:

switch ( op ){
        case '+':
             cout<< num1 << '+' << num2 << '=' << (num1 + num2);
            break;
        case '*':
            cout<< num1 << '*' << num2 << '=' << (num1 * num2);
            break;
        case '-':
            cout<< num1 << '-' << num2 << '=' << (num1 - num2);
            break;
}

image.png

Step 6:

Since division by zero is not possible, we'll write an if and else condition to make sure that we don't get an error.

case '/':
            if (num2 != 0.0)
                cout<< num1 << '/' << num2 << '=' << (num1 / num2);
            else 
                cout<<"divide by zero situation";
            break;

image.png

Step 7:

The last case will be the default case, which will print out the error when the user will enter an invalid operation.

default:
            cout<< op <<"is an invalid operator";

That was all for this program. Hope you enjoyed it.
Thank you so much

Divider 2.png

Steem Infinity Zone Team
@cryptokraze | @arie.steem | @qasimwaqar | @vvarishayy | @suboohi

Footer.png

Click Here to Join Official SIZ Discord Channel

Discord
Twitter
Facebook

Divider 2.png

Sort:  
 3 years ago 

Keep it up dear friend you make a very good post..
Keep learning with our steem fellows and friends

Coin Marketplace

STEEM 0.18
TRX 0.15
JST 0.028
BTC 63099.80
ETH 2455.59
USDT 1.00
SBD 2.58