Answer to Question #195851 in C++ for SWB

Question #195851

1.    Write a C++ program that implements the switch statement to calculate the area either of a circle, rectangle, and triangle. It should use a function CalArea to find the area of chosen shape by the user. (Think and understand what is being demanded)



1
Expert's answer
2021-05-20T00:31:40-0400
#include<iostream>
#include<math.h>
using namespace std;

float calArea(int choice){
  float a,b,c,s,r,area;
  switch(choice){
      case 1:{
        cout<<"Enter the radius of circle: ";
        cin>>r;
        area=3.14*r*r;
        break;
      }
      case 2:{
        cout<<"Enter length and breadth of rectangle";
        cin>>a>>b;
        area=a*b;
        break;
      }
      case 3:{
        cout<<"Enter three side of triangle";
        cin>>a>>b>>c;
        s=(a+b+c)/2;
        area=sqrt(s*(s-a)*(s-b)*(s-c));
        break;
      }
      default: cout<<"Invalid choice";
  }
  return area;
}
int main(){
  int choice;
  cout<<"press 1 for find area of circle\n";
  cout<<"press 2 for find area of rectangle\n";
  cout<<"press 3 for fing area of triangle\n";
  cout<<"Enter your choice";
  cin>>choice;
  float area=calArea(choice);
  cout<<"Area: "<<area<<endl;
  return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog