Find Area of a Triangle Using Its Base and Height: C Program

Find the area of the Triangle when it’s base and height are input by the user.

Related Read:
Find Area of a Triangle Using Its Sides: C Program

Formula To Calculate Area of Triangle when its Base and Height are given

Area = (Base * Height) / 2.0;

Note: If we divide an expression or number by 2, it’ll return only the integer part and the decimal part will be discarded. So we are dividing the expression by 2.0 (which is of type double).

Find Area of a Triangle Using Its Base and Height: C Program



YouTube Link: https://www.youtube.com/watch?v=Vk3sB2G2pI8 [Watch the Video In Full Screen.]


Find Area of a Triangle Using Its Base and Height: C Program

#include < stdio.h >

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

    printf("Enter length of base of Triangle\n");
    scanf("%f", &base);

    printf("Enter length of height of Triangle\n");
    scanf("%f", &height);

    area = (base * height) / 2.0;

    printf("Area of Triangle is %0.2f\n", area);

    return 0;
}

Output:
Enter length of base of Triangle
15
Enter length of height of Triangle
25
Area of Triangle is 187.50

Validate the Input and Find Area of Triangle: C Program

#include < stdio.h >

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

    printf("Enter length of base of Triangle\n");
    scanf("%f", &base);

    printf("Enter length of height of Triangle\n");
    scanf("%f", &height);

    if(base == 0 || height == 0)
    {
        printf("Invalid Input\n");
    }
    else
    {
        area = (base * height) / 2.0;
        printf("Area of Triangle is %0.2f\n", area);
    }

    return 0;
}

Output 1:
Enter length of base of Triangle
0
Enter length of height of Triangle
25
Invalid Input

Output 2:
Enter length of base of Triangle
15
Enter length of height of Triangle
30
Area of Triangle is 225.00

Note: Note that the area of Triangle has only 2 digits after the decimal point. That is because we have %0.2f as format specifier in the printf method where we are printing out the area of Triangle.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert