225k views
5 votes
A piece of wire is to be bent in the form of a rectangle to put around a picture frame. The length of the picture frame is 1.5 times the width. Write a program that prompts the user to input the length of the wire and outputs the length and width of the picture frame.

1 Answer

5 votes

Answer:

The cpp program for the given scenario is shown below.

#include <iostream>

#include <iomanip>

using namespace std;

int main() {

//variables to store length and breadth of the rectangle frame

double length;

double breadth;

//variable to store the length of the wire

double len;

//user prompted to enter the length of the wire

cout<<"Enter the length of the wire: ";

cin>>len;

//computing length of the rectangle frame using area formula

length = len/4.5;

//breadth of the rectangle frame as per given data

breadth = 1.5 * length;

//displaying dimensions of the rectangle frame

cout << "The length and breadth of the picture frame are "<< setprecision(3) << length <<" and "<< setprecision(3) << breadth <<" respectively. "<<endl;

return 0;

}

OUTPUT

Enter the length of the wire: 20

The length and breadth of the picture frame are 4.44 and 6.67 respectively.

Step-by-step explanation:

The program works as described.

1. The variables are declared to store the length and breadth of the rectangle frame and to store the length of the wire.

2. The user is prompted to input the length of the wire, len, as per the question.

3. The length and breadth of the rectangle are calculated using the following steps.

2*(length + breadth) = len

length + (1.5 * length) = len/2

2.5length = len/2

length = len/4.5

4. The dimensions of the rectangle frame are displayed to the user. All the dimensions are displayed with two decimal places. The formatting is done using setprecision(n) where n is the total number of digits required in the output. The value of n can be changed. For the setprecision(n) method, iomanip header file is included.

5. All the variables are declared as double to accommodate all types of numeric values.

6. The program ends with a return statement.

7. The dimensions have no units since this is not mentioned in the question.

8. All coding is written inside main().

User Jnicklas
by
4.1k points