/*create a program with an area function that is overloaded to calcuilate
the area of a circle (given the radius)
the area of rectangle (given length and width)
the area of triangle (given the three side of the triangle)*/
#include <iostream>
#include <cmath>
using namespace std;
#define PI 3.14159
// Area of a circle
double area(double radius) {
return PI * radius * radius;
}
// Area of a rectangle
double area(double length, double width) {
return length * width;
}
// Area of a triangle using Heron's formula
double area(double a, double b, double c) {
double s = (a + b + c) / 2; // semi-perimeter
return sqrt(s * (s - a) * (s - b) * (s - c));
}
int main() {
int choice;
cout << "Choose the shape to calculate the area:" << endl;
cout << "1. Circle" << endl;
cout << "2. Rectangle" << endl;
cout << "3. Triangle" << endl;
cout << "Enter your choice (1/2/3): ";
cin >> choice;
if (choice == 1) {
double radius;
cout << "Enter the radius of the circle: ";
cin >> radius;
cout << "The area of the circle is: " << area(radius) << endl;
}
else if (choice == 2) {
double length, width;
cout << "Enter the length and width of the rectangle: ";
cin >> length >> width;
cout << "The area of the rectangle is: " << area(length, width) << endl;
}
else if (choice == 3) {
double a, b, c;
cout << "Enter the lengths of the three sides of the triangle: ";
cin >> a >> b >> c;
// Check if a valid triangle can be formed with the given sides
if (a + b > c && a + c > b && b + c > a) {
cout << "The area of the triangle is: " << area(a, b, c) << endl;
} else {
cout << "Invalid triangle sides!" << endl;
}
} else {
cout << "Invalid choice!" << endl;
}
return 0;
}