Algorithm
-
- Input base of the triangle. Store in some variable say base.
- Input height of the triangle. Store in some variable say height.
- Use triangle area formula to calculate area i.e.
area = (base * height) / 2
. - Print the resultant value of area.
Area of a triangle
Area of a triangle is given by formula.
Where b is base and h is height of the triangle.
Code Examples
#1 Example-C programing to find the area of a triangle
Code -
C Programming
#include <stdio.h>
#include <math.h> /* It is ecessary for using sqrt function */
void main()
{
/*Variable Declaration*/
float a, b, c, s, area;
/*Taking user input*/
printf("Enter the three sides, a, b, and c, of the triangle:");
scanf("%f%f%f", &a, &b, &c);
/*Calculate the area of the triangle*/
s = (a + b + c) / 2;
area = sqrt((s *(s - a) *(s - b) *(s - c)));
printf("\n The area of the triangle is %f.", area);
}
Copy The Code &
Try With Live Editor
Output
Enter the three sides, a, b, and c, of the triangle:
15
10
15
The area of the triangle is 70.710678.
15
10
15
The area of the triangle is 70.710678.
#2 Example-Programing to find area of a triangle
Code -
C Programming
/**
* C program to find area of a triangle if base and height are given
*/
#include <stdio.h>
int main()
{
float base, height, area;
/* Input base and height of triangle */
printf("Enter base of the triangle: ");
scanf("%f", &base);
printf("Enter height of the triangle: ");
scanf("%f", &height);
/* Calculate area of triangle */
area = (base * height) / 2;
/* Print the resultant area */
printf("Area of the triangle = %.2f sq. units", area);
return 0;
}
Copy The Code &
Try With Live Editor
Input
Enter base of the triangle: 10
Enter height of the triangle: 15
Enter height of the triangle: 15
Output
Area of the triangle = 75 sq. units
Demonstration
C Programing Example to Find Area of Tringle-DevsEnv