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)
#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;
}
Comments
Leave a comment