Python Program to Find Area and Perimeter of a Rhombus
Introduction
A rhombus is a type of quadrilateral with all four sides having equal lengths. It is a special case of a parallelogram where opposite angles are equal, and the diagonals bisect each other at right angles. Understanding its geometric properties helps in solving various mathematical problems.
In this tutorial, we will write a Python program to calculate the area, perimeter, and even the side of a rhombus when provided with relevant inputs. These calculations are based on well-known mathematical formulas.
Formula
- Area: The area of a rhombus can be calculated using the formula:
Area = (Diagonal1 × Diagonal2) / 2
, where Diagonal1 and Diagonal2 are the lengths of the diagonals. - Perimeter: The perimeter is the sum of all sides, given by:
Perimeter = 4 × Side
. - Side: Using the Pythagorean theorem, if the diagonals are known:
Side = √((Diagonal1/2)² + (Diagonal2/2)²)
.
Code
# Python program to calculate the area, perimeter, and side of a rhombus import math # Input the lengths of the diagonals diagonal1 = float(input("Enter the length of Diagonal 1: ")) diagonal2 = float(input("Enter the length of Diagonal 2: ")) # Calculate the area area = (diagonal1 * diagonal2) / 2 # Calculate the side length side = math.sqrt((diagonal1 / 2) ** 2 + (diagonal2 / 2) ** 2) # Calculate the perimeter perimeter = 4 * side # Output the results print("Area of the Rhombus:", area) print("Side of the Rhombus:", side) print("Perimeter of the Rhombus:", perimeter)
Output
Let us assume the diagonals of the rhombus are given as Diagonal1 = 10
units and Diagonal2 = 8
units. The program will prompt the user for these inputs and display the calculated results.
Enter the length of Diagonal 1: 10 Enter the length of Diagonal 2: 8 Area of the Rhombus: 40.0 Side of the Rhombus: 6.4031242374328485 Perimeter of the Rhombus: 25.612496949731394
Explanation
The program first takes the lengths of the diagonals as input from the user. Using the formula for the area of a rhombus, the area is calculated as the product of the diagonals divided by 2. Next, the side of the rhombus is determined using the Pythagorean theorem, where each diagonal is halved to form two right triangles within the rhombus. Finally, the perimeter is computed by multiplying the side length by 4.
The Python math
library is utilized for the square root calculation to ensure accuracy. The program is structured to provide clear, step-by-step outputs that align with the given formulas.
Conclusion
This Python program is a straightforward and efficient way to calculate key geometric properties of a rhombus. By leveraging fundamental mathematical principles and Python’s built-in capabilities, we can easily perform these calculations for any rhombus dimensions provided by the user.