Q:

C program to calculate the area of a triangle given three sides

0

C program to calculate the area of a triangle given three sides

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Read three edges of the triangle, and calculate the area of the triangle using the standard formula.

Formula to calculate the area of a triangle: The given sides are ab, and c.

area of a triangle

Program:

The source code to calculate the area of a triangle given three sides is given below. The given program is compiled and executed using GCC compile on UBUNTU 18.04 OS successfully.

// C program to calculate the area of a triangle
// given three sides

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

float calcuateAreaTriangle(int a, int b, int c)
{
    float s = 0;
    float area = 0;

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

    return area;
}

int main()
{
    int a = 0;
    int b = 0;
    int c = 0;

    float area = 0;

    printf("Enter the First edge of Triangle: ");
    scanf("%d", &a);

    printf("Enter the Second edge of Triangle: ");
    scanf("%d", &b);

    printf("Enter the Third edge of Triangle: ");
    scanf("%d", &c);

    area = calcuateAreaTriangle(a, b, c);

    printf("Area of a triangle: %f\n", area);

    return 0;
}

Output:

RUN 1:
Enter the First edge of Triangle: 2
Enter the Second edge of Triangle: 3
Enter the Third edge of Triangle: 4
Area of a triangle: 2.904737

RUN 2:
nter the First edge of Triangle: 12
Enter the Second edge of Triangle: 10
Enter the Third edge of Triangle: 8
Area of a triangle: 39.686268

RUN 3:
Enter the First edge of Triangle: 24
Enter the Second edge of Triangle: 20
Enter the Third edge of Triangle: 16
Area of a triangle: 158.745071

Explanation:

In the above program, we created two functions calcuateAreaTriangle() and main(). The calcuateAreaTriangle() function is used to calculate the area of the triangle based on the given three edges of the triangle as a parameter.

In the main() function, we read the three edges of the triangle from the user. Then we called the calcuateAreaTriangle() function and got the area of the triangle and printed the result on the console screen.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now