C Program to calculate the perimeter of a rhombus – In this particular article, we will discuss the several ways to calculate the perimeter of a rhombus in C programming.
The methods used to do so are as follows:
- Using Standard Method
- Using Functions
- Using Pointers
- Using Macros
As we all know, a rhombus is a 2-dimensional quadrilateral figure whose sides are all equal in nature. The opposite sides of a rhombus are parallel as well.
A rhombus would look like this:
As you can see, p and q are the two diagonals of the rhombus. Since the diagonals of a rhombus cut each other at right angles, it’s easy to evaluate the side of a rhombus.
Applying the Pythagoras Theorem,
a = sqrt(p^2 + q^2)
Thus, the perimeter of a rhombus = 4a.
C Program to calculate the perimeter of the rhombus in three different ways. Check out our other C Beginner programs. We have written the code in three different ways, with sample outputs and example programs.
The multitude of methods with which the perimeter of a rhombus can be calculated in C Programming is as follows:
Using Standard Method
1)The value of the entered side of rhombus will store into variable”side”
2)Using perimeter=4*side formula, we calculate the perimeter value
1 2 3 4 5 6 7 8 9 10 11 12 |
#include<stdio.h> int main() { float side,perimeter; printf("enter side of rhombus: "); scanf("%f",&side); perimeter=4*side; printf("POS: %f\n",perimeter); return 0; } |
output:
1 2 |
enter side of rhombus: 20 POS: 80.000000 |
Using Functions
1)p=perimeter(s) using this code we are calling the perimeter function.
2)float perimeter(float s) returns the perimeter value and perimeter value will be assigned to p.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include<stdio.h> float perimeter(float s) { return (4*s); } int main() { float s,p; printf("enter side of rhombus: "); scanf("%f",&s); p=perimeter(s); printf("POS: %f\n",p); return 0; } |
output:
1 2 |
enter side of rhombus: 5 POS: 20.000000 |
Using Pointers
1)We are passing the addresses of s,p variables using perimerer(&s,&p) function.
2)void perimeter(float *s, float *p) retrieve the values at that addresses and calculate the perimeter value
3)The perimeter value will be stored into pointer variable *p
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include<stdio.h> void perimeter(float *s,float *p) { *p=((4)*(*s)); } int main() { float s,p; printf("enter side of rhombus: "); scanf("%f",&s); perimeter(&s,&p); printf("POS: %f\n",p); return 0; } |
output:
1 2 |
enter side of rhombus: 10 POS: 40.000000 |
Using Macros
1)Here 4*s formula was defined with the name area(s)
2)area(s) replaced with that expression given at #define.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include<stdio.h> #define area(s) (4*s); int main() { float s,p; printf("enter side of rhombus: "); scanf("%f",&s); p=area(s); printf("POS: %f\n",p); return 0; } |
output:
1 2 |
enter side of rhombus: 112 POS: 448.00000 |