Index « Previous Next »

Question

Suppose a, b, and c denote the lengths of the sides of a triangle. Then the area of the triangle can be calculated using the formula:

area of triangle formula

where s

Write a program that asks the user to input the length of sides of the triangle and print the area.

Source Code

#include <stdio.h>
#include <math.h>

int main()
{
    float a, b, c, s, area;

    printf("Enter the length of side 1 :");
    scanf("%f", &a);
    printf("Enter the length of side 2 :");
    scanf("%f", &b);
    printf("Enter the length of side 3 :");
    scanf("%f", &c);

    s = (a + b + c) / 2;
    area = sqrt(s*(s - a)*(s - b)*(s - c));

    printf("\nThe area of the triangle is %0.2f", area);

    return 0;
}

Output

Enter the length of side 1 :10
Enter the length of side 2 :11
Enter the length of side 3 :13

The area of the triangle is 53.44