C Program to find the area of a rhombus – In this specific article, we will detail in on how to calculate the area of a rhombus in C Programming. The methods used in this particular piece are as follows:
- Using Standard Method
- Using Function
- Using Pointers
- Using Macros
A Rhombus is a quadrilateral whose opposite sides are parallel and all the sides are equal. The diagonals of a rhombus intersect each other at right angles.
The area of a rhombus is calculated with this formula:
A = 1/2 * (Diagonal 1) * (Diagonal 2)
A rhombus looks like this:
As you can see in the image uploaded above, the area of the rhombus can be calculated like this:
Area of ABCD = 1/2 * AC * BD
The different ways of calculating the area of a rhombus in C programming is as follows:
Using Standard Method
1)To calculate the area of Rhombus we need values of two daigonals
2)Diagonal1 will store into d1,diagonal2 store into d2,.
3)d1,d2 will substitute into the formula we will get the area.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include<stdio.h> int main() { float d1,d2,area; printf("enter diagonal1 of rhombus: "); scanf("%f",&d1); printf("enter diagonal1 of rhombus: "); scanf("%f",&d2); area=(d1*d2)/2; printf("AOR: %f\n",area); return 0; } |
1 2 3 |
enter diagonal1 of rhombus: 10 enter diagonal1 of rhombus: 12 AOR: 60.000000 |
Using Function
1)We are calling the function using the code area(d1,d2),here d1,d2 are two diagonals .
2)The called function float area(float d1, float d2) 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 19 20 21 22 |
#include<stdio.h> float area(float d1,float d2) { return (d1*d2)/2; } int main() { float d1,d2,a; printf("enter diagonal1 of rhombus: "); scanf("%f",&d1); printf("enter diagonal2 of rhombus: "); scanf("%f",&d2); a=area(d1,d2); printf("AOR: %f\n",a); return 0; } |
1 2 3 |
enter diagonal1 of rhombus: 5 enter diagonal2 of rhombus: 6 AOR: 15.000000 |
Using Pointers
1)We are passing the references as arguments using the calling function area(&d1,&d2,&a)
2)The called function area(float *d1,float *d2,float *area) will 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 |
#include<stdio.h> void area(float *d1,float *d2,float *area) { *area=((*d1)*(*d2))/2; } int main() { float d1,d2,a; printf("enter diagonal1 of rhombus: "); scanf("%f",&d1); printf("enter diagonal2 of rhombus: "); scanf("%f",&d2); area(&d1,&d2,&a); printf("AOR: %f\n",a); return 0; } |
output:
1 2 3 |
enter diagonal1 of rhombus: 30 enter diagonal2 of rhombus: 40 AOR: 600.000000 |
Using Macros
1)area(d1,d2) is symbolic name to the expression (d1*d2)/2.
2)area(d1,d2)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(d1,d2) (d1*d2)/2; int main() { float d1,d2,a; printf("enter diagonal1 of rhombus: "); scanf("%f",&d1); printf("enter diagonal2 of rhombus: "); scanf("%f",&d2); a=area(d1,d2); printf("AOR: %f\n",a); return 0; } |
1 2 3 |
enter diagonal1 of rhombus: 50 enter diagonal2 of rhombus: 98 AOR: 2450.000000 |