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
}

5 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
  2. Mohsin virk

    Write a c++ program that will:

    • Provide 2 options to the user as below:
     Enter 1 to calculate the area of Rectangle
     Enter 2 to calculate the area of a Trapezoid

    • If the user enters 1, then the program should ask the user to enter the width and length of the rectangle and then show the result after calculation. Formula for calculating Area of rectangle is
    Area = width x Length.

    • If the user enters 2, then the program should ask the user to enter the base1, base2, and height of the trapezoid and then show the result after calculation. Formula for calculating Area of trapezoid is
    Area =

    • You must implement two different functions for calculating areas of rectangle and trapezoid.
    • After taking the choice from the user in form of 1 or 2, the relevant function should be called.
    • After showing the output to the user, you need to ask the user if he/she wants to do another calculation. If the user presses y or Y, then the program should ask the user again to enter the choice of shape otherwise the program should be terminated.

    Screenshot of the program execution is given below for both shapes:

    Sample output:

    Reply
  3. Mohsin virk

    please bro tell me the solution of this problem
    i dont wanna code but instructions please

    Reply

Leave a Reply

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