fork download
  1. // Cenyao Huang CS1A Homework 2, P. 82, #8
  2.  
  3. /**********************************************************************************************************
  4. * COMPUTE TOTALS AND DISPLAY COSTS
  5. * This program computes the total price, subtotal of the sale, the amount of
  6. * sales tax, and displays the cost of each item purchased by the customer.
  7. *
  8. * Computation uses the formula:
  9. * subtotal = item1 + item2 + item3 + item4 + item5
  10. * total = subtotal + subtotal * (salesTax/100)
  11. *
  12. * Input
  13. * item1 : the price of item1
  14. * item2 : the price of item2
  15. * item3 : the price of item3
  16. * item4 : the price of item4
  17. * item5 : the price of item5
  18. * salesTax: the amount of sales tax
  19. * subtotal : the value of the sum of item1, item2, item3, item4, and item5
  20. *
  21. * Output
  22. * item1 : the price of item1
  23. * item2 : the price of item2
  24. * item3 : the price of item3
  25. * item4 : the price of item4
  26. * item5 : the price of item5
  27. * salesTax: the amount of sales tax
  28. * subtotal : the value of the sum of item1, item2, item3, item4, and item5
  29. * total : the value of the subtotal multiplied by salesTax, then added to the subtotal
  30. *
  31. ********************************************************************************************************/
  32. #include <iostream>
  33. #include <cmath>
  34. #include <iomanip>
  35. using namespace std;
  36.  
  37. int main() {
  38. double subtotal; // define variables
  39. double total;
  40. double item1 = 12.95, item2 = 24.95, item3 = 6.95, item4 = 14.95, item5 = 3.95;
  41. double salesTax = 6;
  42.  
  43.  
  44. subtotal = item1 + item2 + item3 + item4 + item5; //calculate item costs and final total
  45. total = subtotal + subtotal * (salesTax/100);
  46.  
  47. cout << "item1 = $" << item1 << endl << "item2 = $" << item2 << endl; //display item prices
  48. cout << "item3 = $" << item3 << endl << "item4 = $" << item4 << endl;
  49. cout << "item5 = $" << item5 << endl;
  50.  
  51. cout << "subtotal = $" << subtotal << endl; //display subtotal, sales tax, and total
  52. cout << "sales tax = " << salesTax << "%" << endl;
  53. cout << "total = $" << fixed << setprecision(2) << total << endl;
  54.  
  55. return 0;
  56. }
Success #stdin #stdout 0.01s 5316KB
stdin
Standard input is empty
stdout
item1 = $12.95
item2 = $24.95
item3 = $6.95
item4 = $14.95
item5 = $3.95
subtotal = $63.75
sales tax = 6%
total = $67.58