w3resource

Python: Reads the two adjoined sides and the diagonal of a parallelogram and check whether the parallelogram is a rectangle or a rhombus

Python Basic - 1: Exercise-49 with Solution

Write a Python program that reads the two adjoining sides and the diagonal of a parallelogram and checks whether the parallelogram is a rectangle or a rhombus.

According to Wikipedia-
parallelograms: In Euclidean geometry, a parallelogram is a simple (non-self-intersecting) quadrilateral with two pairs of parallel sides. The opposite or facing sides of a parallelogram are of equal length and the opposite angles of a parallelogram are of equal measure.
rectangles: In Euclidean plane geometry, a rectangle is a quadrilateral with four right angles. It can also be defined as an equiangular quadrilateral, since equiangular means that all of its angles are equal (360°/4 = 90°). It can also be defined as a parallelogram containing a right angle.
rhombus: In plane Euclidean geometry, a rhombus (plural rhombi or rhombuses) is a simple (non-self-intersecting) quadrilateral whose four sides all have the same length. Another name is equilateral quadrilateral, since equilateral means that all of its sides are equal in length. The rhombus is often called a diamond, after the diamonds suit in playing cards which resembles the projection of an octahedral diamond, or a lozenge, though the former sometimes refers specifically to a rhombus with a 60° angle, and the latter sometimes refers specifically to a rhombus with a 45° angle.

Input:
Two adjoined sides and the diagonal.
1 ≤ ai, bi, ci ≤ 1000, ai + bi > ci
Input two adjoined sides and the diagonal of a parallelogram (comma separated):
3,4,5
This is a rectangle

Sample Solution:

Python Code:

# Prompt the user to input two adjoined sides and the diagonal of a parallelogram (comma separated)
print("Input two adjoined sides and the diagonal of a parallelogram (comma separated):")

# Take user input for the sides and diagonal, and map to integers
a, b, c = map(int, input().split(","))

# Check if the parallelogram is a rectangle using the Pythagorean theorem
if c**2 == a**2 + b**2:
    print("This is a rectangle.")

# Check if the parallelogram is a rhombus by comparing the lengths of the adjoined sides
if a == b:
    print("This is a rhombus.")

Sample Output:

Input two adjoined sides and the diagonal of a parallelogram (comma separated):
 3,4,5
This is a rectangle.

Explanation:

Here is a breakdown of the above Python code:

  • First the code prompts the user to input two adjoined sides and the diagonal of a parallelogram, separated by commas.
  • User input is taken and mapped to integers for the lengths of sides 'a', 'b', and diagonal 'c'.
  • It checks if the parallelogram is a rectangle using the Pythagorean theorem (c^2 = a^2 + b^2).
  • If the condition in step 3 is met, it prints "This is a rectangle."
  • It checks if the parallelogram is a rhombus by comparing the lengths of the adjoined sides ('a' and 'b').
  • If the condition in step 5 is met, it prints "This is a rhombus."

Visual Presentation:

Python: Reads the two adjoined sides and  the diagonal of a parallelogram and check whether the parallelogram is a rectangle or a rhombus.

Flowchart:

Flowchart: Python - Reads the two adjoined sides and  the diagonal of a parallelogram and check whether the parallelogram is a rectangle or a rhombus

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program that reads n digits (given) chosen from 0 to 9 and prints the number of combinations where the sum of the digits equals to another given number (s). Do not use the same digits in a combination.
Next: Write a Python program to replace a string "Python" with "Java" and "Java" with "Python" in a given string.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://198.211.115.131/python-exercises/basic/python-basic-1-exercise-49.php