C Program to Find the area of a triangle – In this stipulated article, we will brief in on the various methods to calculate the area of a triangle. The ways to calculate the area of a triangle in C programming discussed in this distinct article are as follows:
- Using Standard Method
- Using Function
- Using Pointer
- Using Macros
As we all know, triangles are considered to be a closely bound 2-dimensional with the least number of sides, that is 3. There are many properties of a triangle.
The sum of two sides of a triangle can not be more than that of the third side. The sum of all the angles of a triangle is always 180 degrees.
In order to calculate the area of a triangle, we use this specific formula:
Area of a triangle = 1/2 * Base * Height.
A triangle looks like this:
As you can see, calculating the area of the triangle A1A2A3 is extremely easy. The area of the triangle is:
Area of A1A2A3 = 1/2 * A1A2 * A3H3
Thus, the methods discussed in this specific article to find the area of a triangle in C programming are as follows:
Using Standard Method
1)For calculating the area of a triangle we need height and base of a triangle. The base value will substitute into the variable “base” ,height value in the variable “height”.
2)By substituting base and height in the formula we will calculate the area of a triangle.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include<stdio.h> int main() { float base,height,area; printf("enter base of triangle: "); scanf("%f",&base); printf("enter height of the triangle: "); scanf("%f",&height); area=(base*height)/2; printf("AOT: %f\n",area); return 0; } |
output:
1 2 3 |
enter base of triangle: 5 enter height of the triangle: 6 AOT: 15.000000 |
Using Function
1)We are using the function area(float b, float h)to calculate the area of a triangle.
2)area(b,h) calling the function which has float type of arguments.
3)The called function area(float b, float h) will calculate the area and return the value, the return value will be assigned to the variable “a”.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include<stdio.h> float area(float b,float h) { return (b*h)/2; } int main() { float b,h,a; printf("enter base of triangle: "); scanf("%f",&b); printf("enter height of the triangle: "); scanf("%f",&h); a=area(b,h); printf("AOT: %f\n",a); return 0; } |
output:
1 2 3 |
enter base of triangle: 10 enter height of the triangle: 30 AOT: 150.000000 |
Using Pointers
1)We are calling the function by passing references as arguments in the calling function “area(&b,&h,&a)”.
2)The called function void area(float *b, float *h, float *area) calculate the area of a triangle, by retrieving the values at the given addresses and substitute those values in the formula. The calculated value will store into the pointer variable “area”.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include<stdio.h> void area(float *b,float *h,float *area) { *area=((*b)*(*h))/2; } int main() { float b,h,a; printf("enter base of triangle: "); scanf("%f",&b); printf("enter height of the triangle: "); scanf("%f",&h); area(&b,&h,&a); printf("AOT: %f\n",a); return 0; } |
output:
1 2 3 |
enter base of triangle: 4 enter height of the triangle: 8 AOT: 16.000000 |
Using Macros
1)area(b,h) is symbolic name to the expression (b*h)/2.
2)area(b,h) replaced with that expression given at #define.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include<stdio.h> #define area(b,h) (b*h)/2; int main() { float b,h,a; printf("enter base of triangle: "); scanf("%f",&b); printf("enter height of the triangle: "); scanf("%f",&h); a=area(b,h); printf("AOT: %f\n",a); return 0; } |
1 2 3 |
enter base of triangle: 30 enter height of the triangle: 50 AOT: 750.000000 |