218k views
4 votes
A program asks the user to enter the unit price of a chair and the quantity he is buying. It calculates and displays the total price. The unit price is

1 Answer

4 votes

Answer:

The c++ program for the given scenario is shown below.

#include <iostream>

using namespace std;

int main() {

// variables declared for unit price, quantity, total price

// price is taken as double

// quantity is taken as integer

double chair_price, total_price;

int chair_qty;

cout << " Enter the unit price of the chair : " << endl;

cin >> chair_price;

cout << " Enter the number of the chairs to be bought : " << endl;

cin >> chair_qty;

total_price = chair_price * chair_qty;

cout << " The total price for " << chair_qty << " chairs is " << total_price << endl;

return 0;

}

OUTPUT

Enter the unit price of the chair

23.4

Enter the number of the chairs to be bought

10

The total price for 10 chairs is 234

Step-by-step explanation:

1. The variables for unit price, quantity of chairs and total price of chairs are declared. The reason for data type is explained below.

double chair_price, total_price;

int chair_qty;

2. The variables are not initialized since their values is to be entered by the user.

3. First, the user is prompted to enter the unit price of the chair. This is stored in a double variable since price can be in decimals.

cin >> chair_price;

4. Next, the user enters the number of chairs to be bought. This value is stored in an integer variable since quantity can not be a decimal number.

cin >> chair_qty;

5. The total price is computed as the product of the unit price and the quantity of chairs. The total cost is also stored in a double variable since unit price can be in decimals.

total_price = chair_price * chair_qty;

6. Lastly, this total price is displayed to the standard output.

7. The keyword endl is used to insert new line.

User Mattforni
by
8.5k points