Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Program to calculate the area of the rhombus
A rhombus is a four-sided polygon with all equal edges and perpendicular diagonals. It is a special type of parallelogram where opposite sides are equal and parallel.
The area of a rhombus is calculated using its diagonals, as they form four triangles within the figure. The mathematical formula is ?
Area = (p × q) / 2
Where p and q are the lengths of the diagonals.
Input Output Scenarios
Let us look at some input-output scenarios to calculate the area of a rhombus ?
When the product of diagonals is odd:
Input: p = 3, q = 5 Output: Area = 7.5
When the product of diagonals is even:
Input: p = 6, q = 8 Output: Area = 24.0
Using Mathematical Formula
We can implement a Python program that calculates the area of a rhombus using its standard mathematical formula, with the diagonal lengths as input.
Example
The following example shows the Python implementation for computing rhombus area ?
# Length of rhombus diagonals
p = 6
q = 8
# Calculating area of rhombus
area = (p * q) / 2
# Displaying output
print("Area of the Rhombus:", area)
The output of the above code is ?
Area of the Rhombus: 24.0
Using a Function
We can also use user-defined functions to calculate the rhombus area. The def keyword defines a function that contains the calculation logic.
Example
The following code uses a function to find the rhombus area ?
def rhombus_area(p, q):
# Calculating area of rhombus
area = (p * q) / 2
# Returning the calculated area
return area
# Length of rhombus diagonals
p = 6
q = 8
# Calculate and display area
result = rhombus_area(p, q)
print("Area of the Rhombus:", result)
The output is ?
Area of the Rhombus: 24.0
Taking User Input
For a more interactive program, we can take diagonal lengths as user input ?
def calculate_rhombus_area():
# Taking input from user
p = float(input("Enter the length of first diagonal: "))
q = float(input("Enter the length of second diagonal: "))
# Calculating area
area = (p * q) / 2
print(f"Area of the rhombus: {area}")
# Call the function
calculate_rhombus_area()
Conclusion
The area of a rhombus is calculated using the formula (p × q) / 2, where p and q are the diagonal lengths. Python functions make the calculation reusable and organized for different diagonal values.
