Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
Write a program that asks for the length and the width of a rectangle in centimetres, as numbers that may have a decimal point, and shows the area and the perimeter, each with a label. Test it with 12 and 7.5, for which the area is 90.0 and the perimeter is 39.0.
Have you tried it?
Work it out on paper first. It is fine to get stuck or make a mistake: that is how we learn. When you are ready, go through the solution one step at a time, and check each step against your own.
Plan. Input: two decimal numbers. Calculation: area = length * width and perimeter = 2 * (length + width). Output: two labelled lines.
Test. With 12 and 7.5: the area is 12 * 7.5 = 90.0, and the perimeter is 2 * (12 + 7.5) = 39.0. The program shows the same.
Check. Both lines have a label and a unit. The brackets in the perimeter make the sum come first.
Answer: The program above.
Log in to join the discussion.
Loading…