Calculator Program

0 Suresh Chand March 31, 2021

In this example, We will learn to make a simple calculator using c++ programming language. This calculator makes mathematics operations like addition, multiplication, and division. We will use a switch case for this example.

Calculator program using c plus
Calculator program

Let’s look an code

Calculator Program CODE:

In this program, First we will ask for two numbers and one operator. If user provide valid operator then using switch case we will perform operation.

#include <iostream>

using namespace std;


// Main program
int main()

{

    char oper;

    float a, b;

    //We will ask to user for operands
    cout<<"Enter two operands: ";

    cin>> a >> b;

    //We will ask to user to enter operator i.e. +, -, *, /
    cout<<"Enter operator: ";

    cin>> oper;

    switch (oper)

    {

        // If operator is '+'
        case '+':

            cout << a + b;

            break;

        // If operator is '-'
        case '-':

            cout << a - b;

            break;

        // If operator is '*'

        case '*':

            cout << a * b;

            break;

        // If operator is '/'

        case '/':

            cout << a / b;

            break;

        // If any other operator display error message

        default:

            cout << "Please enter valid operator!!!";

            break;

    }

    return 0;

}

The output of above program is

Enter two operands: 12
13
Enter operator: +
25


Enter two operands: 12
13
Enter operator: &
Please enter valid operator!!!
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

Join Our Newsletter Now

Get the latest tutorials, examples and projects updates.

Ads Block Detector Powered by codehelppro.com

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Disable Ad Block

Want Same System