Q:

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

0

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

All Answers

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

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

Program:

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.

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

total answers (1)

C programming Basic Input, Output, if else, Ternary Operator based Programs

Similar questions


need a help?


find thousands of online teachers now
C program to calculate the area of Trapezium... >>
<< C program to calculate the area of a triangle give...