C++ Program to Implement Simple Calculator using Do While Loop

Write C++ Program to Implement Simple Calculator using Do While Loop

// CPP Program to Implement Simple Calculator using Do While Loop

#include <iostream>
#include <stdlib.h>

using namespace std;

int main()
{
    int a, b, i;

    cout << "Enter Number 1 :--> ";
    cin >> a;

    cout << "\nEnter Number 2 :--> ";
    cin >> b;

    do
    {
        cout << "\nEnter 1 for '+'  ";
        cout << "\nEnter 2 for '-'  ";
        cout << "\nEnter 3 for '*'  ";
        cout << "\nEnter 4 for '/'  \n";
        cout << "Enter 0 for Exit ";

        cout << "\nEnter your choice :--> ";
        cin >> i;

        switch(i)
        {
            case 0: break;

            case 1:
                cout <<endl<< a << " + " << b << " = " << a + b;
                break;

            case 2:
                cout <<endl<< a << " - " << b << " = " << a - b;
                break;

            case 3:
                cout <<endl<< a << " * " << b << " = " << a * b;
                break;

            case 4:
                cout <<endl<< a << " / " << b << " = " << a / b;
                break;

            default:
                cout << "\nEnter valid option";
        }
    } while(i != 0);
 }

Output:

Enter Number 1 :--> 8

Enter Number 2 :--> 3

Enter 1 for '+'
Enter 2 for '-'
Enter 3 for '*'
Enter 4 for '/'
Enter 0 for Exit
Enter your choice :--> 1

a + b  = 11

Enter 1 for '+'
Enter 2 for '-'
Enter 3 for '*'
Enter 4 for '/'
Enter 0 for Exit
Enter your choice :--> 2

a - b = 5

Enter 1 for '+'
Enter 2 for '-'
Enter 3 for '*'
Enter 4 for '/'
Enter 0 for Exit
Enter your choice :--> 3

a * b = 24

Enter 1 for '+'
Enter 2 for '-'
Enter 3 for '*'
Enter 4 for '/'
Enter 0 for Exit
Enter your choice :--> 4

a / b = 0.000000

Enter 1 for '+'
Enter 2 for '-'
Enter 3 for '*'
Enter 4 for '/'
Enter 0 for Exit
Enter your choice :--> 0

≪ Previous | Next ≫