Area of an Isosceles Triangle in C – Formula, Code, and Example

The area of an isosceles triangle in C is computed using the Pythagorean theorem: given the base and the length of the two equal sides, you drop a perpendicular from the apex to the midpoint of the base, splitting the triangle into two right triangles. The height h = sqrt(side² − (base/2)²), and the area equals (1/2) × base × height. This combines math.h functions with simple input validation — a good exercise for beginners moving from formula to code.

How It Works — Step by Step

Given base = 6 and equal side = 5:

  1. Half the base: 6 / 2 = 3
  2. Height via Pythagorean theorem: sqrt(5² − 3²) = sqrt(25 − 9) = sqrt(16) = 4
  3. Area = (1/2) × 6 × 4 = 12.0000

Validity check: the equal side must be greater than half the base, otherwise the apex would fall at or below the base line and no triangle can form.

C Program for Area of an Isosceles Triangle

/* Area of an isosceles triangle in C
 * Compile: gcc -ansi -Wall -Wextra triangle.c -o triangle -lm */
#include <stdio.h>
#include <math.h>

int main(void)
{
    double base, side, half, height, area;

    printf("Enter the base length: ");
    scanf("%lf", &base);

    printf("Enter the equal side length: ");
    scanf("%lf", &side);

    half = base / 2.0;

    if (side <= half) {
        printf("Error: side must be greater than half the base.\n");
        return 1;
    }

    /* Height via Pythagorean theorem: h = sqrt(side^2 - (base/2)^2) */
    height = sqrt(side * side - half * half);
    area   = 0.5 * base * height;

    printf("Height = %.4f\n", height);
    printf("Area   = %.4f\n", area);

    return 0;
}

How to Compile and Run

gcc -ansi -Wall -Wextra triangle.c -o triangle -lm
./triangle

The -lm flag links the math library needed for sqrt().

Sample Input and Output

Test 1 — base 6, equal side 5:

Enter the base length: 6
Enter the equal side length: 5
Height = 4.0000
Area   = 12.0000

Test 2 — base 10, equal side 13:

Enter the base length: 10
Enter the equal side length: 13
Height = 12.0000
Area   = 60.0000

Test 3 — invalid triangle:

Enter the base length: 4
Enter the equal side length: 2
Error: side must be greater than half the base.

Code Explanation

  • half = base / 2.0 — computes the distance from the base midpoint to either base corner. Used in both the validity check and the height formula.
  • sqrt(side * side – half * half) — applies the Pythagorean theorem to the right triangle formed by the height, half the base, and the equal side. Declared in <math.h>.
  • Validity check — if side <= half, the perpendicular height would be zero or imaginary, meaning no real triangle exists. The program exits early with an error message.
  • scanf with %lf — reads a double. Using %f with double in scanf is undefined behaviour in C89; %lf is correct.

What This Program Teaches

  • math.h and sqrt() — calling a standard math function and linking with -lm on Linux/macOS.
  • Input validation before computation — checking preconditions before calling functions that would produce incorrect or undefined results.
  • double precision arithmetic — using double and %lf for geometry calculations where rounding errors in float would matter.
  • Pythagorean theorem in code — translating a mathematical formula directly into a C expression.

Related Programs

Recommended book:
The C Programming Language — Kernighan & Ritchie (India) |
(US)
 | 
C Programming: A Modern Approach — K.N. King (India) |
(US)

Practice what you learned: C Aptitude Questions — or try our C Programming Quiz App on Android.

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>