Area of Triangle

CCBeginner
Practice Now

Introduction

In geometry, the area of a triangle is defined as the amount of space inside the boundary of the triangle. There are several ways to calculate the area of a triangle, but two of the most common methods are using the base and height of the triangle or using Heron's formula which takes the three sides of the triangle as input.

In this lab, you will learn how to write C programs to find the area of a triangle using both of these methods.

Basic Program using Base and Height

The following program calculates the area of a triangle using the base and height of the triangle.

#include<stdio.h>
int main()
{
    int h, b;
    float area;

    // Input height and base of the triangle
    printf("Enter the height of the Triangle: ");
    scanf("%d", &h);
    printf("Enter the base of the Triangle: ");
    scanf("%d", &b);

    // Calculate area of the triangle
    area = (h*b)/(float)2;

    // Output the area of the triangle
    printf("The area of the triangle is: %f", area);
    return 0;
}

Explanation:

  • We have included stdio.h library for standard input-output functions.
  • We have defined the main function and declared some variables h, b, and area.
  • We have taken input from the user for the base and height of the triangle using the scanf function.
  • Then we calculate the area of the triangle using the formula (height x base)/2.
  • area = (h*b)/(float)2 gives the result in decimal points due to the typecasting of the denominator value from int to float.
  • Finally, we output the area of the triangle using the printf function.

Advanced Program using Heron's Formula

The following program calculates the area of a triangle using Heron's formula.

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

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

    // Input sides of the triangle
    printf("Enter the sides of the triangle:\n");
    scanf("%lf%lf%lf", &a, &b, &c);

    // Calculate s, the semi-perimeter of the triangle
    s = (a+b+c)/2;

    // Calculate area of the triangle using Heron's formula
    area = sqrt(s*(s-a)*(s-b)*(s-c));

    // Output the area of the triangle
    printf("The area of the Triangle calculated using Heron's formula is: %lf", area);
    return 0;
}

Explanation:

  • We have included stdio.h and math.h library for standard input-output functions and square root function, respectively.
  • The main function and some variables are declared.
  • We input the three sides of the triangle using the scanf function.
  • We calculate the semi-perimeter of the triangle s using the formula (a+b+c)/2.
  • Using Heron's formula, we calculate the area of the triangle area = sqrt(s*(s-a)*(s-b)*(s-c)).
  • Finally, we output the area of the triangle using the printf function.

Writing the Code in main.c

Now, create a new file main.c in the ~/project/ directory and copy the code from previous steps.

Running the Code

To run the code, simply open a terminal and navigate to the ~/project/ directory and type the following commands:

gcc main.c -o main
./main

Full Code of main.c

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

int main()
{
    // Step 1: Basic Program using Base and Height
    int h, b;
    float area;
    printf("Enter the height of the Triangle: ");
    scanf("%d", &h);
    printf("Enter the base of the Triangle: ");
    scanf("%d", &b);
    area = (h*b)/(float)2;
    printf("The area of the triangle is: %f\n", area);

    // Step 2: Advanced Program using Heron's Formula
    double a, b, c, area2, s;
    printf("Enter the sides of the triangle:\n");
    scanf("%lf%lf%lf", &a, &b, &c);
    s = (a+b+c)/2;
    area2 = sqrt(s*(s-a)*(s-b)*(s-c));
    printf("The area of the Triangle calculated using Heron's formula is: %lf\n", area2);

    return 0;
}

Summary

In this lab, you have learned how to write C programs to find the area of a triangle using both the base and height method and Heron's formula method. You have also learned how to write code in the main.c file and compile and run the program in the terminal.

Other C Tutorials you may like