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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL c(("`C`")) -.-> c/UserInteractionGroup(["`User Interaction`"]) c(("`C`")) -.-> c/BasicsGroup(["`Basics`"]) c(("`C`")) -.-> c/PointersandMemoryGroup(["`Pointers and Memory`"]) c(("`C`")) -.-> c/FunctionsGroup(["`Functions`"]) c/UserInteractionGroup -.-> c/output("`Output`") c/BasicsGroup -.-> c/comments("`Comments`") c/BasicsGroup -.-> c/variables("`Variables`") c/BasicsGroup -.-> c/data_types("`Data Types`") c/BasicsGroup -.-> c/operators("`Operators`") c/UserInteractionGroup -.-> c/user_input("`User Input`") c/PointersandMemoryGroup -.-> c/memory_address("`Memory Address`") c/PointersandMemoryGroup -.-> c/pointers("`Pointers`") c/FunctionsGroup -.-> c/function_declaration("`Function Declaration`") c/FunctionsGroup -.-> c/math_functions("`Math Functions`") subgraph Lab Skills c/output -.-> lab-123199{{"`Area of Triangle`"}} c/comments -.-> lab-123199{{"`Area of Triangle`"}} c/variables -.-> lab-123199{{"`Area of Triangle`"}} c/data_types -.-> lab-123199{{"`Area of Triangle`"}} c/operators -.-> lab-123199{{"`Area of Triangle`"}} c/user_input -.-> lab-123199{{"`Area of Triangle`"}} c/memory_address -.-> lab-123199{{"`Area of Triangle`"}} c/pointers -.-> lab-123199{{"`Area of Triangle`"}} c/function_declaration -.-> lab-123199{{"`Area of Triangle`"}} c/math_functions -.-> lab-123199{{"`Area of Triangle`"}} end

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