Saturday 5 September 2020

Part One: Exercise 2

 In this exercise, we would write  a code to calculate the area and the perimeter of rectangle.

The area of a rectangle is given as length multiplied by the width.

The perimeter of a rectangle is given as the length + length + width + width or 2(length + width).

We could include a title like A PROGRAM TO CALCULATE THE AREA AND PERIMETER OF A RECTANGLE.

We would have output as follows:

Length is …. cm

Width is …. cm

Area is … sq. cm

Perimeter is … cm

Thank you.

You can compare yours with our solution below:

print('A PROGRAM TO CALCULATE THE AREA AND PERIMETER OF A RECTANGLE')

length = 10

width = 5

area = length * width

perimeter = 2*(length + width)

 

print('Length is ',length,'cm')

print('width is ',width,'cm')

print('Area is ',area,'sq. cm')

print('Perimter is ',perimeter,'cm')

print()

print('Thank you')


That is it. The program above would calculate the area and perimeter of a rectangle with length 10 cm and width 5 cm. You can of course vary these parameters.

Note that whatever you enclose within quotation marks including spaces are printed as it is because Python treats it as a string.

Also, the print() command would print a blank line in the output.

You can vary this code to find the area of a triangle or that of a square.

Next would be exercise 3.


No comments:

Post a Comment