C++ Program to Find Area, Perimeter, and Side of a Rhombus
Introduction
A rhombus is a type of quadrilateral where all four sides have equal lengths. It is commonly known as a diamond shape. In geometry, calculating properties such as the area, perimeter, and side length is crucial for understanding this figure. The diagonal lengths play a significant role in these calculations.
In this program, we will create a C++ code to calculate the area, perimeter, and the side length of a rhombus using the lengths of its diagonals.
Formulas
To perform the calculations for a rhombus, we use the following formulas:
- Area: The area of a rhombus can be calculated using its diagonals (
d1
andd2
). The formula is:Area = (d1 × d2) / 2
. - Perimeter: The perimeter is the sum of all the sides of the rhombus. Since all sides are equal, the formula becomes:
Perimeter = 4 × side
. - Side Length: Using the Pythagorean theorem, we can determine the length of a side when the diagonals are known:
Side = √((d1/2)² + (d2/2)²)
.
Code
Below is the C++ program to calculate the area, perimeter, and side length of a rhombus:
#include <iostream> #include <cmath> using namespace std; int main() { // Variables to store the lengths of diagonals double d1, d2; // Input the diagonals cout << "Enter the length of the first diagonal (d1): "; cin >> d1; cout << "Enter the length of the second diagonal (d2): "; cin >> d2; // Calculate the area of the rhombus double area = (d1 * d2) / 2; // Calculate the side length of the rhombus double half_d1 = d1 / 2; double half_d2 = d2 / 2; double side = sqrt((half_d1 * half_d1) + (half_d2 * half_d2)); // Calculate the perimeter of the rhombus double perimeter = 4 * side; // Display the results cout << "Area of the rhombus: " << area << " square units" << endl; cout << "Side length of the rhombus: " << side << " units" << endl; cout << "Perimeter of the rhombus: " << perimeter << " units" << endl; return 0; }
Output
Here is an example of how the program works with sample inputs:
Enter the length of the first diagonal (d1): 10 Enter the length of the second diagonal (d2): 8 Area of the rhombus: 40 square units Side length of the rhombus: 6.40312 units Perimeter of the rhombus: 25.6125 units
Explanation
The program begins by taking the lengths of the two diagonals (d1
and d2
) as input from the user. These values are used to calculate:
- Area: The diagonals are multiplied and divided by 2 to compute the area.
- Side Length: Each diagonal is halved, and the Pythagorean theorem is applied to find the side length of the rhombus.
- Perimeter: The side length is multiplied by 4 to calculate the total perimeter.
The results are displayed to the user, providing a clear understanding of the rhombus's geometric properties.