Lets write a C program to calculate area of a Triangle when their sides are input by the user. Lets use pointers and functions.
Problem Statement
If the lengths of the sides of a Triangle are denoted by a, b and c, then area of Triangle is given by:
where, s = (a + b + c) / 2. Write a function to calculate the area of triangle.
Here s is semi-perimeter of the Triangle.
Related Read:
Find Area of a Triangle Using Its Sides: C Program
Basics of Pointers In C Programming Language
Formula To Calculate Area of a Triangle When its Sides are Given
area = sqrt( S * (S – a) * (S – b) * (S – c) );
where a, b and c are lengths of sides of the Triangle.
S = (a + b + c) / 2.0;
S – Semi-perimeter.
Very Important Note:
Any address preceded by a * (Indirection operator) will fetch the value present at that address.
Video Tutorial: C Program To Calculate Area of Triangle using Pointers
[youtube https://www.youtube.com/watch?v=cfQhWAoP6u8]
Source Code: C Program To Calculate Area of Triangle using Pointers
#include<stdio.h> #include<math.h> void cal_area(float, float, float, float*); int validate(float, float, float); int main() { float a, b, c, area; printf("Enter values of 3 sides of a Triangle.\n"); scanf("%f%f%f", &a, &b, &c); if( validate(a, b, c) ) { cal_area(a, b, c, &area); printf("Area of Triangle is %0.2f\n", area); } else { printf("Please enter valid values for sides of Triangle.\n"); } return 0; } void cal_area(float x, float y, float z, float *A) { float S; S = (x + y + z) / 2.0; *A = sqrt(S * (S - x) * (S - y) * (S - z)); } int validate(float x, float y, float z) { int flag = 0; if(x > y && x > z) { flag = ( x < (y + z) ); } else if(y > z) { flag = ( y < (x + z) ); } else { flag = ( z < (x + y) ); } return(flag); }
Output 1:
Enter values of 3 sides of a Triangle.
10
20
30
Please enter valid values for sides of Triangle.
Output 2:
Enter values of 3 sides of a Triangle.
50
30
0
Please enter valid values for sides of Triangle.
Output 3:
Enter values of 3 sides of a Triangle.
15
14
2
Area of Triangle is 12.53
Output 4:
Enter values of 3 sides of a Triangle.
14.6
14
0.7
Area of Triangle is 2.58
Output 5:
Enter values of 3 sides of a Triangle.
4
5
6
Area of Triangle is 9.92
In Output 1: the largest side is 30. So the other side is 10 and 20. Adding 10 and 20 gives 30, which is not greater than the largest side. So these three values can’t form a valid triangle.
In Output 2: we’ve a 0. No side of a Triangle can have a length of 0. So it’s a invalid Triangle.
Logic To Calculate Area of a Triangle using Pointers
We ask the user to enter/input values of 3 sides of a Triangle. We pass these three values along with the address of variable area to a function called cal_area().
Inside cal_area() function we calculate the semi-perimeter using below formula:
S = (x + y + z) / 2.0;
Note: While calculating Semi-perimeter make sure to divide by 2.0 and not by 2. Because sum of sides of triangle might be a floating point number. Division by integer will give integer value as result. So it’s always better to avoid division by integer value.
Calculate Area of Triangle using Heron’s or Hero’s Formula
To calculate Area of a Triangle when all 3 of its sides are known, we make use of Heron’s or Hero’s Formula:
*A= sqrt( S * (S – a) * (S – b) * (S – c) );
We are storing the result inside a pointer variable. Variable A has the address of variable area i.e., A = &area. *A is the value present at address A or &area.
When we modify the value present at an address it reflects everywhere in the program. Hence we are able to display the result of area inside main method, without literally returning any value from cal_area() funtion.
Logic To Validate The Sides of Triangle
A Triangle is said to be valid if the sum of two sides is greater than the largest of the three sides.
For Example:
If a, b and c are 3 sides of the Triangle. If c is the largest side. Then for the Triangle to be valid, value of (a+b) must be greater than c.
(a+b) > c
Triangle Valid or Not based On Sides: C Program
In function validate() we first find the largest side of the triangle and check if its value is less than the sum of other 2 sides of the triangle. If true we return 1 else we return 0.
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