Find the Area of a Triangle in C

CCBeginner
Practice Now

Introduction

In this lab, you will learn how to calculate the area of a triangle using C programming. The lab consists of three main steps: reading the base and height of the triangle, computing the area using the formula Area = 0.5 _ Base _ Height, and printing the calculated area. By the end of this lab, you will have a solid understanding of how to perform basic geometry calculations in C.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL c(("`C`")) -.-> c/UserInteractionGroup(["`User Interaction`"]) c(("`C`")) -.-> c/BasicsGroup(["`Basics`"]) c(("`C`")) -.-> c/FunctionsGroup(["`Functions`"]) c/UserInteractionGroup -.-> c/output("`Output`") c/BasicsGroup -.-> c/variables("`Variables`") c/UserInteractionGroup -.-> c/user_input("`User Input`") c/FunctionsGroup -.-> c/math_functions("`Math Functions`") subgraph Lab Skills c/output -.-> lab-435181{{"`Find the Area of a Triangle in C`"}} c/variables -.-> lab-435181{{"`Find the Area of a Triangle in C`"}} c/user_input -.-> lab-435181{{"`Find the Area of a Triangle in C`"}} c/math_functions -.-> lab-435181{{"`Find the Area of a Triangle in C`"}} end

Read Base and Height

In this step, you will learn how to read the base and height of a triangle using C programming. We'll create a simple program that allows user input for the triangle's dimensions.

First, let's create a new C file in the project directory:

cd ~/project
nano triangle_area.c

Now, enter the following code to read base and height:

#include <stdio.h>

int main() {
    float base, height;

    // Prompt user to enter base
    printf("Enter the base of the triangle: ");
    scanf("%f", &base);

    // Prompt user to enter height
    printf("Enter the height of the triangle: ");
    scanf("%f", &height);

    // Print the input values
    printf("Base: %.2f\n", base);
    printf("Height: %.2f\n", height);

    return 0;
}

Example output:

Enter the base of the triangle: 5.5
Enter the height of the triangle: 4.2
Base: 5.50
Height: 4.20

Let's break down the code:

  • We use float to store decimal numbers for base and height
  • printf() is used to display prompts to the user
  • scanf() reads the user's input for base and height
  • %.2f formats the output to show two decimal places

Compile the program:

gcc triangle_area.c -o triangle_area

Example output:

(no output if compilation is successful)

Run the program:

./triangle_area

Compute Area = 0.5 _ Base _ Height

In this step, you will learn how to calculate the area of a triangle using the formula: Area = 0.5 _ Base _ Height. We'll modify the previous program to compute and display the triangle's area.

Open the existing file:

cd ~/project
nano triangle_area.c

Update the code to calculate the area:

#include <stdio.h>

int main() {
    float base, height, area;

    // Prompt user to enter base
    printf("Enter the base of the triangle: ");
    scanf("%f", &base);

    // Prompt user to enter height
    printf("Enter the height of the triangle: ");
    scanf("%f", &height);

    // Calculate area
    area = 0.5 * base * height;

    // Print input values and calculated area
    printf("Base: %.2f\n", base);
    printf("Height: %.2f\n", height);
    printf("Area of the triangle: %.2f\n", area);

    return 0;
}

Compile the updated program:

gcc triangle_area.c -o triangle_area

Example output:

(no output if compilation is successful)

Run the program:

./triangle_area

Example output:

Enter the base of the triangle: 5.5
Enter the height of the triangle: 4.2
Base: 5.50
Height: 4.20
Area of the triangle: 11.55

Let's break down the area calculation:

  • We added a new float variable area
  • The formula area = 0.5 * base * height calculates the triangle's area
  • printf() displays the calculated area with two decimal places

Print the Area

In this final step, we'll enhance the program to provide a more user-friendly output and format the area printing.

Open the existing file:

cd ~/project
nano triangle_area.c

Update the code to improve area printing:

#include <stdio.h>

int main() {
    float base, height, area;

    // Prompt user to enter base
    printf("Triangle Area Calculator\n");
    printf("----------------------\n");
    printf("Enter the base of the triangle: ");
    scanf("%f", &base);

    // Prompt user to enter height
    printf("Enter the height of the triangle: ");
    scanf("%f", &height);

    // Calculate area
    area = 0.5 * base * height;

    // Print formatted output
    printf("\nCalculation Results:\n");
    printf("-------------------\n");
    printf("Base:   %.2f\n", base);
    printf("Height: %.2f\n", height);
    printf("Area:   %.2f square units\n", area);

    return 0;
}

Compile the updated program:

gcc triangle_area.c -o triangle_area

Example output:

(no output if compilation is successful)

Run the program:

./triangle_area

Example output:

Triangle Area Calculator
----------------------
Enter the base of the triangle: 6.0
Enter the height of the triangle: 4.0

Calculation Results:
-------------------
Base:   6.00
Height: 4.00
Area:   12.00 square units

Key improvements:

  • Added descriptive headers for input and output
  • Formatted output with clear labels
  • Added "square units" to clarify the area measurement
  • Improved readability with consistent formatting

Summary

In this lab, you will learn how to read the base and height of a triangle using C programming, and then calculate the area of the triangle using the formula: Area = 0.5 _ Base _ Height. The program prompts the user to enter the base and height, and then computes and displays the triangle's area.

The key learning points are: 1) using printf() to display prompts to the user, 2) using scanf() to read the user's input for base and height, 3) calculating the area using the formula Area = 0.5 * Base * Height, and 4) formatting the output to show two decimal places.

Other C Tutorials you may like