C Program to Find the Area and Perimeter of an Isosceles Triangle
Introduction
An isosceles triangle is a triangle that has two sides of equal length and two equal angles opposite those sides. It is one of the most common types of triangles, with unique geometric properties that make its area and perimeter easy to compute.
This tutorial explains how to calculate the area and perimeter of an isosceles triangle using a C program. The tutorial will include mathematical formulas, the C code implementation, output examples, and a detailed explanation.
Formula
The formulas to compute the area and perimeter of an isosceles triangle are as follows:
-
Perimeter: The perimeter is the sum of all three sides:
Perimeter = 2 × equal_side + base
Here, equal_side is the length of the two equal sides, and base is the length of the third side. -
Area: The area of an isosceles triangle can be calculated using the formula:
Area = (base × height) / 2
The height is calculated using the Pythagorean theorem as:Height = √(equal_side² - (base / 2)²)
These formulas allow us to compute the area and perimeter when the lengths of the base and the equal sides are known.
C Code
The following C program calculates the area and perimeter of an isosceles triangle:
#include <stdio.h> #include <math.h> int main() { float equal_side, base, height, area, perimeter; // Input from the user printf("Enter the length of the two equal sides of the isosceles triangle: "); scanf("%f", &equal_side); printf("Enter the length of the base of the isosceles triangle: "); scanf("%f", &base); // Calculate height using the Pythagorean theorem height = sqrt((equal_side * equal_side) - ((base / 2) * (base / 2))); // Calculate area area = (base * height) / 2; // Calculate perimeter perimeter = 2 * equal_side + base; // Display results printf("Perimeter of the isosceles triangle: %.2f\n", perimeter); printf("Area of the isosceles triangle: %.2f\n", area); return 0; }
Output
When the program is executed, it prompts the user to input the lengths of the equal sides and the base. Below is a sample run of the program:
Enter the length of the two equal sides of the isosceles triangle: 5 Enter the length of the base of the isosceles triangle: 6 Perimeter of the isosceles triangle: 16.00 Area of the isosceles triangle: 12.00
In this example:
- The equal sides were entered as 5, and the base was entered as 6.
- The program calculated the height using the formula
√(5² - (6 / 2)²)
, which is approximately 4.00. - It then calculated the area as
(6 × 4) / 2 = 12
and the perimeter as2 × 5 + 6 = 16
.
Explanation
The program follows these steps:
- Input: The program prompts the user to input the lengths of the two equal sides and the base of the isosceles triangle.
-
Height Calculation: Using the Pythagorean theorem, the height of the triangle is calculated. The height divides the triangle into two right-angled triangles, allowing the use of:
Height = √(equal_side² - (base / 2)²)
-
Area Calculation: The formula
(base × height) / 2
is used to calculate the area of the triangle. -
Perimeter Calculation: The perimeter is computed by summing up the lengths of all three sides using
2 × equal_side + base
. - Output: The calculated area and perimeter are displayed with two decimal places for clarity.
Key Points
- An isosceles triangle has two equal sides and two equal angles, making it a symmetric shape.
- The height divides the base into two equal halves, forming two congruent right-angled triangles.
- The program uses the
math.h
library for thesqrt
function to compute the square root during height calculation. - The area and perimeter formulas rely on basic geometric principles, ensuring accurate calculations.