Answer on Question #81799 – Programming & Computer Science | C++
A bagel shop charges 75 cents per bagel for orders of less than a half-dozen bagels and 50 cents per bagel for orders of a half-dozen or more. Write a program that requests the number of bagels ordered and displays the price for each bagel and the total cost. Format the output results with two decimal places.
Answer:
#include <iostream>
#include <iomanip>
using namespace std;
// constants
const int HALF_DOZEN = 6;
const double PRICE = 0.75;
const double LOW_PRICE = 0.50;
int main()
{
int number_of_bagels; // the number of bagels ordered
double total_price;
double price_per_bagel = PRICE;
// read the input
cout << "Enter the number of bagels ordered: ";
cin >> number_of_bagels;
// define the price per bagel
if (number_of_bagels >= HALF_DOZEN) {
price_per_bagel = LOW_PRICE;
}
// calculate the total price
total_price = price_per_bagel * number_of_bagels;
// output results
cout << fixed << setprecision(2); // set output format for doubles with 2 decimal places
cout << "Price for each bagel: " << price_per_bagel << endl;
cout << "Total price: " << total_price << endl;
return 0;
}See the next page for a screenshot
D:\google\GF>g++ bagel_shop.cpp -o bagel_shop.exe
D:\google\GF>bagel_shop.exe
Enter the number of bagels ordered: 2
Price for each bagel: 0.75
Total price: 1.50
D:\google\GF>bagel_shop.exe
Enter the number of bagels ordered: 5
Price for each bagel: 0.75
Total price: 3.75
D:\google\GF>bagel_shop.exe
Enter the number of bagels ordered: 6
Price for each bagel: 0.50
Total price: 3.00
D:\google\GF>bagel_shop.exe
Enter the number of bagels ordered: 10
Price for each bagel: 0.50
Total price: 5.00Answer provided by www.AssignmentExpert.com
Comments