C Program to Calculate the perimeter of a rectangle – In this distinct article, we will brief in on all the ways you can calculate the perimeter of a rectangle in C Programming.
The ways that are used to do so in this blog are as follows:
- Using Standard Method
- Using Functions
- Using Pointers
- Using Macros
In this piece, we have added sample programs and suitable examples for your easy understanding. The compiler has been added as well for a better understanding.
As we all know, a rectangle is a 2-dimensional quadrilateral figure whose opposite sides are equal and parallel. All the angles of a rectangle are also equal to 90 degrees.
A rectangle looks like this:
As you can see, since the opposite sides of a rectangle are equal, the perimeter of a rectangle can be calculated with this formula:
Perimeter = l + b + l + b = 2 (l + b)
The multiple methods mentioned are as follows:
Using Standard Method
1)The formula for the perimeter of a rectangle is perimeter=2*(length+breadth)
2)We are calculating the perimeter by substituting the length and breadth values into related variables “length”, “breadth”, the calculated value will store into “perimeter” variable.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include<stdio.h> int main() { float length,breadth,perimeter; printf("enter length of rectangle: "); scanf("%f",&length); printf("enter breadth of rectangle: "); scanf("%f",&breadth); perimeter=2*(length+breadth); printf("AOR: %f\n",perimeter); return 0; } |
1 2 3 |
enter length of rectangle: 10 enter breadth of rectangle: 20 AOR: 60.000000 |
Using Functions
1)Here we are using the function float perimeter(float l,float b) to calculate perimeter,which returns the float value.
2)We are calling the function using perimeter(l,b)where l,b are float type arguments
3)float perimeter(float l, float b) calculate the perimeter and return the value, that value will store into variable “p”
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 perimeter(float l,float b) { return 2*(l+b); } int main() { float l,b,p; printf("enter length of rectangle: "); scanf("%f",&l); printf("enter breadth of rectangle: "); scanf("%f",&b); p=perimeter(l,b); printf("POR: %f\n",p); return 0; } |
1 2 3 |
enter length of rectangle: 2 enter breadth of rectangle: 5 POR: 14.000000 |
Using Pointers
1)Here *p,*l,*b are pointer variables
2)we are calling the function using perimeter(&l,&b,&p), here we are passing addresses
3)void perimeter(float *l,float *b,float *p) function calculate the perimeter ,the value will store into pointer variable *p
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 perimeter(float *l,float *b,float *p) { *p=2*(*l+*b); } int main() { float l,b,p; printf("enter length of rectangle: "); scanf("%f",&l); printf("enter breadth of rectangle: "); scanf("%f",&b); perimeter(&l,&b,&p); printf("POR: %f\n",p); return 0; } |
1 2 3 |
enter length of rectangle: 10 enter breadth of rectangle: 25 POR: 70.000000 |
Using Macros
1)The formula for the perimeter of rectangle 2*(l+b) was defined with the name perimeter(l,b).
2)perimeter(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 perimeter(l,b) 2*(l+b); int main() { float l,b,p; printf("enter length of rectangle: "); scanf("%f",&l); printf("enter breadth of rectangle: "); scanf("%f",&b); p=perimeter(l,b); printf("POR: %f\n",p); return 0; } |
1 2 3 |
enter length of rectangle: 13 enter breadth of rectangle: 23 POR: 72.000000 |