C program to calculate the area of a triangle given base and height

Here, we are going to learn how to calculate the area of a triangle given base and height using C program?
Submitted by Nidhi, on August 05, 2021

Problem statement

Read the base and height of the triangle from the user, and calculate the area of the triangle.

Area of triangle formula = (base x height)/2

C program to calculate the area of a triangle given base and height

The source code to calculate the area of a triangle 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 base and height

#include <stdio.h>

int calcuateAreaTriangle(int base, int height)
{
    return (base * height) / 2;
}

int main()
{
    int base = 0;
    int height = 0;
    int area = 0;

    printf("Enter the base of Triangle: ");
    scanf("%d", &base);

    printf("Enter the height of Triangle: ");
    scanf("%d", &height);

    area = calcuateAreaTriangle(base, height);

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

    return 0;
}

Output

RUN 1:
Enter the base of Triangle: 10
Enter the height of Triangle: 26
Area of a triangle: 130

RUN 2:
Enter the base of Triangle: 2
Enter the height of Triangle: 4
Area of a triangle: 4

RUN 3:
Enter the base of Triangle: 12
Enter the height of Triangle: 7
Area of a triangle: 42

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 base and height of the triangle as a parameter.

In the main() function, we read the base and height 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.

C Basic Programs »

Related Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.