Tuesday, September 12, 2017

C++ Code Review: Area Calculator

 

Description: This program shows how to draw menu and calculate area of Circle, Triangle, and Rectangle individually. Uses switch case to draw menu.

Code:

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

//Program calculates area of given object.

 

#include<iostream>

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

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

#include<cstdlib> //for system()

 

using namespace std;

 

int main()

{

    float a,b,c,s,r,area;

    int ch;

 

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

 

    cout << endl;

    cout<<"***Area Calculator Program***"<< endl;

    cout<<"   1. Circle"<< endl;

    cout<<"   2. Triangle"<< endl;

    cout<<"   3. Rectangle"<< endl;

    cout<<"   4. Quit"<< endl;

    cout<<"Enter your Choice:_";

    cin>>ch;

 

    switch(ch)

    {

    case 1:

    {

        cout<<"   To calculate Area of Circle"<< endl;

        cout<<"   Enter radius :";

        cin>>r;

        area=3.14*r*r;

        break;

    }

 

    case 2:

    {

        cout<<"   To calculate Area of Triangle"<< endl;

        cout<<"   Enter three sides:";

        cin>>a>>b>>c;

        s=(a+b+c)/2;

        area=sqrt(s*(s-a)*(s-b)*(s-c));

        break;

    }

 

    case 3:

    {

        cout<<"   To calculate Area of Square"<< endl;

        cout<<"   Enter length and breadth:";

        cin>>a>>b;

        area=a*b;

        break;

    }

    case 4:

    {

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

        exit(0);

    }

    default:

        goto top;

    }

 

    cout<<"   Area = "<< area ;

 

    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