Call us at 0700-922-6559 or Click here

Write a C++ program to compute area of right angle triangle, equilateral triangle, isosceles triangle using function overloading concept.

#include <iostream>

using namespace std;

// Overloaded functions to calculate area of different triangles

float area(float base, float height) {

  // Area of right angle triangle

  return 0.5 * base * height;   }

float area(float side) {

  // Area of equilateral triangle

  return (sqrt(3) / 4) * side * side;    }

float area(float equalSides, float base) {

  // Area of isosceles triangle

  float height = sqrt(equalSides * equalSides – (base / 2) * (base / 2));

  return 0.5 * base * height;   }

int main() {

  float base, height, side;

  // Calculate area of right angle triangle

  cout << “Enter base and height of right angle triangle: “;

  cin >> base >> height;

  cout << “Area of right angle triangle: ” << area(base, height) << endl;

  // Calculate area of equilateral triangle

  cout << “Enter side of equilateral triangle: “;

  cin >> side;

  cout << “Area of equilateral triangle: ” << area(side) << endl;

  // Calculate area of isosceles triangle

  cout << “Enter base and equal side of isosceles triangle: “;

  cin >> base >> equalSides;

  cout << “Area of isosceles triangle: ” << area(equalSides, base) << endl;

  return 0;

}

Output: 

Enter base and height of right angle triangle: 10 8

Area of right angle triangle: 40

Enter side of equilateral triangle: 6

Area of equilateral triangle: 15.5885

Enter base and equal side of the isosceles triangle: 8 5

Area of isosceles triangle: 10

Here’s a step-by-step explanation of the code:

1. Header and Namespace:

  • #include <iostream>: Includes the iostream header for input/output operations.
  • using namespace std;: Brings the std namespace into scope for convenient use of elements like cin, cout, and endl.

2. Overloaded area Functions:

  • float area(float base, float height) { … }: Calculates the area of a right-angled triangle using the formula 0.5 * base * height.
  • float area(float side) { … }: Calculates the area of an equilateral triangle using the formula (sqrt(3) / 4) * side * side.
  • float area(float equalSides, float base) { … }: Calculates the area of an isosceles triangle:
    • First, it calculates the height using the formula sqrt(equalSides * equalSides – (base / 2) * (base / 2)).
    • Then, it uses the formula 0.5 * base * height to find the area.

3. Main Function:

  • int main(): The program’s entry point.
    • float base, height, side;: Declares variables for triangle dimensions.
    • // Calculate area of right angle triangle:
      • Prompts the user for base and height.
      • Calls the area(base, height) function to calculate and print the area.
    • // Calculate area of equilateral triangle:
      • Prompts the user for the side.
      • Calls the area(side) function to calculate and print the area.
    • // Calculate area of isosceles triangle:
      • Prompts the user for base and equal side.
      • Calls the area(equalSides, base) function to calculate and print the area.
    • return 0;: Indicates successful program termination.

Leave a Reply

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