In this tutorial you will learn about the C Program to Find Area of a Triangle and its application with practical example.
C Program to Find Area of a Triangle
In this tutorial, we will learn to create a C program that will Find the Area of a Triangle in C programming.
Prerequisites
Before starting with this tutorial, we assume that you are the best aware of the following C programming topics:
- Operators in C Programming.
- Basic Input and Output function in C Programming.
- Basic C programming.
- Mathematical operations in C programming.
Algorithm:-
1 2 3 4 5 6 7 8 9 |
1. Declaring the required variables for the program. 2. Taking the input three sides of the triangle. 3. Calculating the area of the triangle using the code. 4. Printing the area of a triangle. 5. End the program. |
Program description:-
In today’s tutorial, we will create a program, that will calculate the area of a triangle using C programming. First, take the input three sides of the triangle by the user. Then we will calculate the area of that triangle using the mathematical expression. At last, we will print the area of a triangle.
Program Code:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
/* C Program to find Area of a Triangle */ //including the required header files. #include <stdio.h> #include <math.h> int main() { //Declaring the required variable for the program. double x, y, z, sum, area; //x,y,z it will hold the sides of the triangle. //area it will hold the value of area. //taking the input sides from the user for the program printf("Enter sides of a triangle\n"); //scanning the sides of the triangle. scanf("%lf%lf%lf", &x, &y, &z); sum = (x+y+z)/2; // Semiperimeter //calculating the area of the right angled triangle. area = sqrt(sum*(sum-x)*(sum-y)*(sum-z)); //Printing the Area of right angled triangle printf("Area of the triangle = %.2lf\n", area); return 0; } |
Output:-
In the above program, we have first initialized the required variable.
- x = it will hold the integer value for the input side of the triangle.
- y = it will hold the integer value for the input side of the triangle.
- z = it will hold the integer value for the input side of the triangle.
- area = it will hold the area of the triangle.
Taking Input the three sides length the triangle.
Code to calculate the area of the triangle.
Printing output the area of the triangle.