C Program to find the area of a trapezium. In this particular article, we will brief in on the different ways the area of a trapezium is calculated in C programming. The methods discussed here are as follows:
- Using Standard Method
- Using Function
- Using Pointers
- Using Macros
A Trapezium is a quadrilateral which has a single pair of parallel opposite sides. A square, rhombus and a rectangle can be considered as trapeziums since these have at least one pair of parallel opposite sides.
The area of a trapezium after simple derivation can be considered to be
= 1/2 * (sum of the length of the parallel sides) * height.
A trapezium is given in the following image:
As you can see, the area of the trapezium shown in the image can be easily calculated. The area is:
1/2 * (4 + 10) * 8 = 56 sq. units.
Using Standard Method
1)For the area of a trapezium, we need the values of base1,base2, height.
2)By substituting the values of base1,base2,height into the formula area=((base1+base2)/2)*height,we will get area of trapezium,the value will store into the variable “area”.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include<stdio.h> int main() { float base1,base2,height,area; printf("enter base1 of parallelogram: "); scanf("%f",&base1); printf("enter base2 of parallelogram: "); scanf("%f",&base2); printf("enter height of the parallelogram: "); scanf("%f",&height); area=((base1+base2)/2 )*height; printf("AOP: %f\n",area); return 0; } |
1 2 3 4 |
enter base1 of parallelogram: 10 enter base2 of parallelogram: 15 enter height of the parallelogram: 20 AOP: 250.000000 |
Using Function
1)We are using float area(float a, float b, float h) function to calculate the area of the trapezium.
2)We are calling the function using area(a,b,h), then called function will calculate the area of the trapezium and return the value.
3)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 19 20 21 |
#include<stdio.h> float area(float a,float b,float h) { return ( ((a+b)/2) * h); } int main() { float b,h,a,A; printf("enter a: "); scanf("%f",&a); printf("enter b: "); scanf("%f",&b); printf("enter h: "); scanf("%f",&h); A=area(a,b,h); printf("AOT: %f\n",A); return 0; } |
output:
1 2 3 4 |
enter a: 3 enter b: 5 enter h: 4 AOT: 16.000000 |
Using Pointers
1)We are passing the addresses as arguments in the calling function AOT(&b1,&b2,&h,&area).
2)The called function AOT(float *a,float *b,float *h,float *area) calculate the area by retrieving the values at that addresses.
3)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 21 22 23 |
#include<stdio.h> void AOT(float *a,float *b,float *h,float *area) { *area=((*a+*b)/2)*(*h); } int main() { float b1,b2,h,area; printf("enter base1: "); scanf("%f",&b1); printf("enter base2: "); scanf("%f",&b2); printf("enter height: "); scanf("%f",&h); AOT(&b1,&b2,&h,&area); printf("AOT: %f\n",area); return 0; } |
output:
1 2 3 4 |
enter base1: 10 enter base2: 15 enter height: 30 AOP: 375.000000 |
Using Macros
1)area(b1,b2,h) is symbolic name to the expression ((b1+b2)/2)*h
2)area(b1,b2,h)replaced with that expression given at #define.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include<stdio.h> #define area(b1,b2,h) ((b1+b2)/2)*h; int main() { float b1,b2,h,a; printf("enter base1: "); scanf("%f",&b1); printf("enter base2: "); scanf("%f",&b2); printf("enter height: "); scanf("%f",&h); a=area(b1,b2,h); printf("AOT: %f\n",a); return 0; } |
1 2 3 4 |
enter base1: 12 enter base2: 14 enter height: 20 AOT: 260.000000 |