Write a C++ Calculator to perform Arithmetic Operations

in cpp •  3 years ago  (edited)

Through this coding model, you will figure it out "How to Make a Calculator in C++?" This will be a basic calculator, capable of carrying out the four basic arithmetic errands.

The program starts by inciting the client for choosing an arithmetic activity among the 4 accessible choices. Then, at that point, it plays out the activity followed by showing the outcome as the yield of the program. The program exits execution here. In this manner, since you get an overall thought, here's the actual switch in c++:

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
float num_1,num_2;
int operation;

cout<<"What Arithmetic Operation do you want to perform:\n";
cout<<"Press 1 for Addition \n" ;
cout<<"Press 2 for Subtraction\n";
cout<<"Press 3 for Multiplication\n";
cout<<"Press 4 for Division\n";
cin>>operation;

cout<<"Now Enter Two Numbers\n";
cin>>num_1>>num_2;

switch (operation)

    {
    case 1:
    cout<<"The Addition result is: "<<num_1+num_2;
    break;

    case 2:
    cout<<"The Subtraction result is: "<<num_1-num_2;
    break;

    case 3:
    cout<<"The Multiplication result is: "<<num_1*num_2;
    break;

    case 4:
    cout<<"The Division result is: "<<num_1/num_2;
    break;
    }
getch();
}

Sample Output:

What Arithmetic Operation do you want to perform:

Press 1 for Addition
Press 2 for Subtraction
Press 3 for Multiplication
Press 4 for Division

4

Now Enter Two Numbers

100
30

The Division result is: 3.33333
#include <stdio.h>

int main() 
{
    char operator;
    double num1, num2;
    //ask user to enter the operator
    printf("Select the operator (+, -, *, /): ");
    scanf("%c", &operator);

    //ask user to enter two numbers
    printf("Enter two numbers (eg. 12 3): ");
    scanf("%lf %lf",&num1, &num2);

    //start switch case
    switch(operator)
    {   //if operator is addition
        case '+':
            printf("%.1lf + %.1lf = %.1lf",num1, num2, num1+num2);
            break;

        // if operator is substraction
        case '-':
            printf("%.1lf - %.1lf = %.1lf",num1, num2, num1-num2);
            break;

        //if operator is multiplication
        case '*':
            printf("%.1lf * %.1lf = %.1lf",num1, num2, num1*num2);
            break;

        //if operator is division
        case '/':
            printf("%.1lf / %.1lf = %.1lf",num1, num2, num1/num2);
            break;

        // if user enter a wrong operaotr
        default:
            printf("Please select the correct operator");
    }

    return 0;
}

Output

Select the operator (+, -, *, /): *
Enter two numbers (eg. 12 3): 4 5
4.0 * 5.0 = 20.0

Conclusion

That summarizes this showing to perform arithmetic tasks utilizing a switch case in C++. With the previously mentioned code, we have fostered a basic calculator that can perform expansion, subtraction, multiplication, and division.

Note that this is just a single method of composing this program. You can likewise attempt to compose it as you discover best. The more choices you'll investigate, the better you will learn. The very best!

P.S. – Oh indeed, you can likewise parade your code for a similar program by means of the dedicated comments section underneath.

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  
Loading...