```
Question: Write c++ programe to calculate the area of Circle, Rectangle, Triangle and Square.
Answer:
#include <iostream>
#include <cmath>
using namespace std;
// Function to calculate the area of a circle
double circleArea(double radius) {
if (radius < 0)
return -1; // Error handling for negative radius
return 3.14159 * radius * radius;
}
// Function to calculate the area of a rectangle
double rectangleArea(double length, double breadth) {
if (length < 0 || breadth < 0)
return -1; // Error handling for negative side lengths
return length * breadth;
}
// Function to calculate the area of a triangle
double triangleArea(double base, double height) {
if (base < 0 || height < 0)
return -1; // Error handling for negative side lengths
return 0.5 * base * height;
}
// Function to calculate the area of a square
double squareArea(double side) {
if (side < 0)
return -1; // Error handling for negative side length
return side * side;
}
// Function to display the area based on user selection
void showResults(int choice, double result) {
switch (choice) {
case 1:
cout << " Circle Area " << endl;
cout << "Circle Area = " << result << endl;
break;
case 2:
cout << " Rectangle Area " << endl;
cout << "Rectangle Area = " << result << endl;
break;
case 3:
cout << " Triangle Area " << endl;
cout << "Triangle Area = " << result << endl;
break;
case 4:
cout << " Square Area " << endl;
cout << "Square Area = " << result << endl;
break;
default:
cout << "Invalid Choice" << endl;
}
}
int main() {
int choice;
double result;
do {
cout << " WELCOME TO MY Area Calculator \n\n " << endl;
cout << " \nArea Calculator\n ";
cout << "1. Circle\n";
cout << "2. Rectangle\n";
cout << "3. Triangle\n";
cout << "4. Square\n";
cout << "5. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
double radius;
cout << "Enter radius to your circle : ";
cin >> radius;
result = circleArea(radius);
break;
case 2:
double length, breadth;
cout << "Enter length and breadth to your rectangle Area : ";
cin >> length >> breadth;
result = rectangleArea(length, breadth);
break;
case 3:
double base, height;
cout << "Enter base and height to your Triangle Area : ";
cin >> base >> height;
result = triangleArea(base, height);
break;
case 4:
double side;
cout << "Enter side length to your Square Area : ";
cin >> side;
result = squareArea(side);
break;
case 5:
break;
default:
cout << "Invalid Choice\n";
}
if (choice != 5 && result != -1) {
showResults(choice, result);
}
} while (choice != 5);
return 0;
}
```

##### About Me

##### Most Popular

##### Categories

##### Featured Posts

Could not find posts.

##### Newsletter

## Join Our Newsletter

Enter your email and we'll keep you posted with news and updates!

###### You might also like

## Join Our Newsletter

Enter your email and we'll keep you posted with news and updates!

## Write Comment