Thursday, September 21, 2017

C++ Code Review: Mathematics operation on two given number

Description: This program shows how to draw menu and apply mathematics operation such as addition, subtraction, multiplication and division on two given numbers. The switch case constructor is used to draw menu.

Code:

//Compiling Tool: Code::Block IDE with MinGW

//Program uses basic maths operators to do sum calculations.

 

#include<iostream>

#include<conio.h> //for getch()

#include<math.h>  //for sqrt()

#include<cstdlib> //for system()

 

using namespace std;

 

int main()

{

    int a,b,result;

    int ch;

 

top:system("cls");//to clear the screen

 

    cout << endl;

    cout<<"***Basic Maths Calculator Program***"<< endl;

    cout<<"   1. Addition"<< endl;

    cout<<"   2. Subtraction"<< endl;

    cout<<"   3. Multiplication"<< endl;

    cout<<"   4. Division"<< endl;

    cout<<"   5. Modulus"<< endl;

    cout<<"   6. Quit"<< endl;

    cout<<"Enter your Choice:_";

    cin>>ch;

 

    if(ch < 6)

        {

        cout<<"   Enter two variables:_" ;

        cin>>a>>b;

        }

    switch(ch)

    {

    case 1:

    {

        result = a + b;

        break;

    }

 

    case 2:

    {

        result = a - b;

        break;

    }

 

    case 3:

    {

        result = a * b;

        break;

    }

 

    case 4:

    {

        if(b<=0)

        {

            cout << "division not possible as b<=0";

            result = 0;

        }

        else

            result = a / b;

        break;

    }

 

    case 5:

    {

        if(b<=0)

        {

            cout << "modulus not possible as b<=0";

            result = 0;

        }

        else

            result = a% b;

        break;

    }

 

    case 6:

    {

        cout<<"   Quitting the program"<< endl;

        exit(0);

    }

    default:

        goto top;

    }

 

    cout<<"   result = "<< result ;

 

    getch(); //to stop the screen

    goto top;

}

Output:

 clip_image002

If you want to write or construct or program C++ mini-project and do not know how or from where to start buy this simple e-book: Code Review of 26 C++ mini-projects. For book sample click this link.

Add your comments in below available comment box.

Note: Click these label/tags to view all related posts. Tags: C++Code

 

…till next post, bye-bye & take care.

No comments:

Post a Comment