C++ program to calculate area of a circle,a rectangle or a triangle depending upon user's choice

By | July 10, 2012

#include<iostream.h>
#include<conio.h>
#include<math.h>

void main()
{
clrscr(); //to clear the screen
float a,b,c,s,r,area;
int ch;
cout<<“***Menu***n1.Area of circlen2.Area of Rectangle”;
cout<<“n3.Area of trianglenEnter your choice:”;
cin>>ch;

switch(ch)
{
case 1:
{
cout<<“nEnter radius of the circle:”;
cin>>r;
area=3.14*r*r;
break;
}
case 2:
{
cout<<“nEnter length and breadth:”;
cin>>a>>b;
area=a*b;
break;
}
case 3:
{
cout<<“nEnter three sides of the triangle:”;
cin>>a>>b>>c;
s=(a+b+c)/2;
area=sqrt(s*(s-a)*(s-b)*(s-c));
break;
}
default: cout<<“nWrong choice…!!!”;
break;
}

cout<<“Area=”<<area;
getch(); //to stop the screen
}


You May Also Like:

3 thoughts on “C++ program to calculate area of a circle,a rectangle or a triangle depending upon user's choice

  1. Ori

    That was great.
    Can you please tell me what I did wrong here??

    //Give the Area of a Square or a Triangle.
    #include // tells the compiler to include this file
    #include // tells the compiler to include this file
    #include// tells the compiler to include this file
    #include// tells the compiler to include this file
    using namespace std; //use the standart namespace

    float findSArea(float s); // function that calculates Square area
    float findTArea(float b, float h); // function that calculates Triangle area

    int main() // start of main function
    {

    float side, base, height; //input: data for calc
    float square, triangle;
    char letter;
    char S,T;
    float num;

    cout<<"Please enter S for Square or T for Triangle (in capital letters): "<>letter;

    // decide what to do
    if ((letter==S) || (letter==T)) {

    if (letter==S) {
    cout<>side;
    num=findSArea(s); // function call to calculate the Square area
    cout<>num;
    }
    if (letter==T) {
    cout<>base>>height;
    num=findTArea(b,h); // function call to calculate the Triangle area
    cout<>num;
    }
    }
    else
    {
    cout<< "invalid character! ";
    }

    system ("pause");
    return 0; }

    // Compute the area of a Square with side s
    //Pre: s is defined
    //Post: returns area

    float findSArea(float s)
    {return (pow (s,2)); }

    // Compute the area of a triangle with base b & height h
    //Pre: b & H are defined
    //Post: returns area

    float findTArea(float b,h)
    {return (0.5 * b * h); }

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *