Another program in the list of C Programs – C program to find the area of a parallelogram, with sample outputs and sample example code. Do check it out.
In this appropriate article, we will learn the numerous methods to calculate the area of a parallelogram in C programming. The ways to calculate the area of a parallelogram in C Programming are as follows:
- Using Standard Method
- Using Function
- Using Pointers
- Using Macros
As we all know, a parallelogram is a 2-dimensional closed figure made up of four sides. There is a multitude of characteristics of a parallelogram.
A parallelogram is a quadrilateral whose opposite sides are equal and parallel to each other. The opposite angles of a parallelogram are equal in nature.
The sum of all the angles of a parallelogram is equal to 360 degrees.
A parallelogram looks like this:
The formula to calculate the area of a parallelogram = Base * Height
The height of a parallelogram is equal to the minimum distance between the opposite sides.
The various methods used to calculate the area of a parallelogram in C programming are as follows:
Using Standard Method
1)By calculating the area of parallelogram we need a base and height.
2)We declared the variables base, height, area.
3)Substitute the values into the formula area=(base*height), then we will get area value, that will be assigned to the variable “area”.
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 parallelogram: "); scanf("%f",&base); printf("enter height of the parallelogram: "); scanf("%f",&height); area=(base*height); printf("AOP: %f\n",area); return 0; } |
output:
1 2 3 |
enter base of parallelogram: 10 enter height of the parallelogram: 20 AOP: 200.000000 |
Using Function
1)We are calling the function as area(b,h), here we are passing the b,h values as arguments.
2)The called function area(float b, float h) will calculate the area and returns 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 |
#include<stdio.h> float area(float b,float h) { return (b*h); } int main() { float b,h,a; printf("enter base: "); scanf("%f",&b); printf("enter height: "); scanf("%f",&h); a=area(b,h); printf("AOP: %f\n",a); return 0; } |
output:
1 2 3 |
enter base: 30 enter height: 40 AOP: 1200.000000 |
Using Pointers
1)We are calling the function by passing references as arguments using area(&b,&h,&a).
2)The called function will calculate the area, which is having pointers as arguments.
3)The calculated area will be assigned to 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)); } int main() { float b,h,a; printf("enter base: "); scanf("%f",&b); printf("enter height: "); scanf("%f",&h); area(&b,&h,&a); printf("AOP: %f\n",a); return 0; } |
output:
1 2 3 |
enter base: 15 enter height: 20 AOP: 300.000000 |
Using Macros
1)Here area(b,h) is the symbolic name to the expression (b*h).
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 16 17 18 19 20 21 |
#include<stdio.h> #define area(b,h) (b*h); int main() { float b,h,a; printf("please enter base: "); scanf("%f",&b); printf("please enter height: "); scanf("%f",&h); a=area(b,h); printf("AOP: %f\n",a); return 0; } |
1 2 3 |
please enter base: 23 please enter height: 32 AOP: 736.000000 |