SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Tony Gaddis
Chapter:
Decision Structures And Boolean Logic
Exercise:
Programming Exercises
Question:2 | ISBN:9780132576376 | Edition: 2

Question

Areas of Rectangles
The area of a rectangle is the rectangle’s length times its width. Write a program that asks for the length and width of two rectangles. The program should tell the user which rectangle has the greater area, or if the areas are the same.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

CODE:

Length_1 = float(input("Enter the length of the 1st rectangle:"))       #Enter the length of 1st rectangle.
Width_1 = float(input("Enter the width of the 1st rectangle:"))         #Enter the width of 1st rectangle.
Area_1 = Length_1 * Width_1                                             #Calculate Area of 1st rectangle.
Length_2 =  float(input("Enter the length of the 2nd rectangle:"))     #Enter the length of 2nd rectangle.
Width_2 = float(input("Enter the width of the 2nd rectangle:"))        #Enter the width of 2nd rectangle.
Area_2 = Length_2 * Width_2                                            #Calculate Area of 2nd rectangle.

if Area_1 > Area_2:                                                   #find which rectangle has the greater area.
    print(" Rectangle_1 has the greater area of ",Area_1)              #display the result.
elif Area_2 > Area_1:
    print(" Rectangle_2 has the greater area of ",Area_2)
elif Area_1 == Area_2:                                                #Check if the areas are the same.
    print("Rectangle_1 and Rectangle_2 have the same area of",Area_1)  #dislay teh result.
    

OUTPUT:

Enter the length of the 1st rectangle:1
Enter the width of the 1st rectangle:2
Enter the length of the 2nd rectangle:3
Enter the width of the 2nd rectangle:4
 Rectangle_2 has the greater area of  12.0


Enter the length of the 1st rectangle:4
Enter the width of the 1st rectangle:3
Enter the length of the 2nd rectangle:3
Enter the width of the 2nd rectangle:4
Rectangle_1 and Rectangle_2 have the same area of 12.0

 

0 0

Discussions

Post the discussion to improve the above solution.