In this post, we will learn how to calculate the perimeter of a rhombus using the C Programming language.
A rhombus is a quadrilateral whose four sides all have the same length. The opposite sides of the rhombus are parallel in nature.
Since all the sides of the rhombus have equal length, then its perimeter will be 4 * length of one side.
We will use the following approaches to find the perimeter of the rhombus:
- Using Standard Method
- Using Functions
- Using Pointers
So, without further ado, let’s begin this tutorial.
C Program to Calculate Perimeter of Rhombus
// C Program to Calculate Perimeter of Rhombus #include <stdio.h> int main(){ float side, perimeter; // Asking for input printf("Enter the side of the rhombus: "); scanf("%f", &side); // Calculating perimeter of rhombus perimeter = 4 * side; // Display output printf("The Perimeter of the rhombus = %.2f", perimeter); return 0; }
Output
Enter the side of the rhombus: 7.2
The perimeter of the rhombus = 28.80
How Does This Program Work?
float side, perimeter;
In this program, we have declared two floating data type variables named side and perimeter.
// Asking for input printf("Enter the side of the rhombus: "); scanf("%f", &side);
The user is asked to enter the length of the side of the rhombus. The entered value gets stored in the side named variable.
// Calculating perimeter of rhombus perimeter = 4 * side;
The perimeter of the rhombus is calculated using the formula: perimeter = 4 x Length of Side of Rhombus.
// Display output printf("Perimeter of the rhombus = %.2f", perimeter);
The perimeter of the rhombus is printed on the screen using the printf() function. We have used %.2f format specifier to limit the perimeter to 2 decimal places.
C Program to Calculate Perimeter of Rhombus Using Functions
// C Program to Calculate Perimeter of Rhombus Using Functions #include <stdio.h> float rhombusPerimeter(float s){ return 4 * s; } int main(){ float side, result; // Asking for input printf("Enter the side of the rhombus: "); scanf("%f", &side); // Calling out custom function result = rhombusPerimeter(side); // Display output printf("The Perimeter of the rhombus = %.1f", result); return 0; }
Output
Enter the side of the rhombus: 5.4
The perimeter of the rhombus = 21.6
C Program to Calculate Perimeter of Rhombus Using Pointers
// C Program to Calculate Perimeter of Rhombus Using Pointers #include <stdio.h> void perimeter(float *s, float *p){ *p = (4 * (*s)); } int main(){ float s, p; // Asking for input printf("Enter the length of the side: "); scanf("%f", &s); perimeter(&s, &p); // Display output printf("The Perimeter of the rhombus is = %.2f", p); return 0; }
Output
Enter the length of the side: 12.1
The perimeter of the rhombus is = 48.40
Conclusion
I hope after going through this post, you understand how to calculate the perimeter of a rhombus using the C Programming language.
If you have any doubts regarding the program, then contact us in the comment section. We will be delighted to assist you.
Also Read: