C program to find the area of a triangle

Write a C program to input base and height of a triangle from user and find area of the triangle given. How to find area of a triangle whose base and height is given by user in C programming. C program to calculate area of a triangle if base and height of the triangle are given.

Input base of the triangle: 10
Input height of the triangle: 15
Output area = 75 sq. units

Required knowledge

Fundamentals of C, Data types, Talking user input in C

Area of triangle

Area of triangle can be given by formula: (base * height) / 2.


 * C program to find area of a triangle if base and height are given

#include <stdio.h>

int main()
    float base, height, area;

    // Reads base and height of the triangle from user
    printf("Enter base of triangle: ");
    scanf("%f", &base);
    printf("Enter height of triangle: ");
    scanf("%f", &height);

    // Calculates area of triangle
    area = (base * height) / 2;

    printf("Area of the triangle = %.2f sq. units.", area);

    return 0;

Enter base of triangle: 10
Enter height of triangle: 15
Area of the triangle = 75.00 sq. units.

Note: %.2f is used to print the area only up to two decimal places. You can also use simply %f to print area up to six decimal places.

Happy coding ;)

Any doubt or suggestion write here. I will try my best to help. Before posting your code you must escape it to view. To format your source code and use format highlighting, post your source code inside
< code >< pre > -- Your source code -- < /pre >< /code > (Remove spaces from pre and code tags).

No comments:

Post a Comment