In this article we will learn to implement a C program to calculate the area of a triangle. A C program is provided below to read the three sides of triangle and display the area of the triangle as output.
//C program to calculate the area of a triangle
//Formula: area = (s(s-a)(s-b)(s-c))1/2, where s=(a+b+c)/2. This formula is Heron's formula
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float a, b, c, s, area;
printf("Enter side a: ");
scanf("%f", &a);
printf("Enter side b: ");
scanf("%f", &b);
printf("Enter side c: ");
scanf("%f", &c);
s = (a+b+c)/2;
area = sqrt(s*(s-a)*(s-b)*(s-c));
printf("Area of the triangle is: %.3f", area);
getch();
}
Input and output for the above program is as follows:
Enter side a: 5
Enter side b: 6
Enter side c: 8
Area of the triangle is: 14.981
Suryateja Pericherla, at present is a Research Scholar (full-time Ph.D.) in the Dept. of Computer Science & Systems Engineering at Andhra University, Visakhapatnam. Previously worked as an Associate Professor in the Dept. of CSE at Vishnu Institute of Technology, India.
He has 11+ years of teaching experience and is an individual researcher whose research interests are Cloud Computing, Internet of Things, Computer Security, Network Security and Blockchain.
He is a member of professional societies like IEEE, ACM, CSI and ISCA. He published several research papers which are indexed by SCIE, WoS, Scopus, Springer and others.
Leave a Reply