C Program to find the area of a rectangle – In this particular article, we will discuss how to calculate the area of a rectangle in C Programming. The methods used in this specific article are as follows:
- Using Standard Method
- Using Function
- Using Pointers
- Using Macros
As we all know, rectangles are extremely commonly used quadrilaterals. In a rectangle, the opposite sides are equal and parallel. Essentially, every square is a rectangle since its opposite sides are equal and parallel as well.
The area of a rectangle can be calculated as:
Area = Length * Breadth
A rectangle looks like this:
As you can see, the area of this very rectangle can be calculated very easily. The area of this rectangle is:
Area = 9 * 4 = 36 sq. units.
Thus, the different methods with which the area of a rectangle can be calculated in C programming are as follows:
Using Standard Method
1)We declared the variables length,breadth,area.
2)By substituting the values into the formula area=(length*breadth), we will get the value of area which is assigned to the variable “area”.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include<stdio.h> int main() { float length,breadth,area; printf("enter length of rectangle: "); scanf("%f",&length); printf("enter breadth of rectangle: "); scanf("%f",&breadth); area=(length*breadth); printf("AOR: %f\n",area); return 0; } |
1 2 3 |
enter length of rectangle: 10 enter breadth of rectangle: 12 AOR: 120.000000 |
Using Function
1)We are using the function area(float l, float b) to calculate the area of a rectangle.
2)Using the code area(l,b) we are calling the function. here area(l,b) is calling function.
3)The called function area(float l, float b) will calculate the area and return the value, that 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 22 |
#include<stdio.h> float area(float l,float b) { return (l*b); } int main() { float l,b,a; printf("enter length of rectangle: "); scanf("%f",&l); printf("enter breadth of rectangle: "); scanf("%f",&b); a=area(l,b); printf("AOR: %f\n",a); return 0; } |
output:
1 2 3 |
enter length of rectangle: 12 enter breadth of rectangle: 13 AOR: 156.000000 |
Using Pointers
1)We are passing the references as arguments using the calling function area(&l,&b,&a).
2)The called function area(float *l, float *b, float *area) will calculate the area of a rectangle, which is having pointer variables as arguments.
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 |
#include<stdio.h> void area(float *l,float *b,float *area) { *area=((*l)*(*b)); } int main() { float l,b,a; printf("enter length of rectangle: "); scanf("%f",&l); printf("enter breadth of rectangle: "); scanf("%f",&b); area(&l,&b,&a); printf("AOR: %f\n",a); return 0; } |
1 2 3 |
enter length of rectangle: 15 enter breadth of rectangle: 16 AOR: 240.000000 |
Using Macros
1)area(l,b) is symbolic name to the expression (l*b).
2)In the code area(l,b) 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(l,b) (l*b); int main() { float l,b,a; printf("enter length of rectangle: "); scanf("%f",&l); printf("enter breadth of rectangle: "); scanf("%f",&b); a=area(l,b); printf("AOR: %f\n",a); return 0; } |
1 2 3 |
enter length of rectangle: 20 enter breadth of rectangle: 30 AOR: 600.000000 |