Chapter 2 – Q #8: Total Purchase – Tony Gaddis – Starting Out With C++

 Chapter 2 – Q #8:  Total Purchase – Tony Gaddis – Starting Out With C++






Total Purchase


Problem:-
A customer in a store is purchasing five items. The prices of the five items are:
  • Price of item 1 = $12.95
  • Price of item 2 = $24.95
  • Price of item 3 = $6.95
  • Price of item 4 = $14.95
  • Price of item 5 = $3.95
Write a program that holds the prices of the five items in five variables. Display each items price, the subtotal of the sale, the amount of sales tax, and the total. Assume the sales tax is 6%.

Solution:-

#include <iostream>

#include<iomanip>

using namespace std;

int main()

{

      const double

            item1 = 12.95,

            item2 = 24.95,

            item3 = 6.95,

            item4 = 14.95,

            item5 = 3.95;

     

      double subtotal = item1 + item2 + item3 + item4 + item5;

      double sales_tax = (6 / 100.0) * subtotal;

      double total = subtotal + sales_tax;

 

      cout << setprecision(2) << fixed;

      cout << "Sales Tax:  " << sales_tax << endl;

      cout << "Sub-Total:  " << subtotal << endl;

      cout << "Total Sale: " << total << endl;

 

      return 0;

}


This is the solution of this question


Explanation of this Solution


  • First, I declare five constant item1, item2, item3, item4 and item5 and initialize the given values
  • Second, I declare subtotal and add five items.
  • Third, I declare sales tax and calculate value.
  • Fourth, I declare total variable and add subtotal and sale tax.
  • Fifth, use setpreicision for point value after point.   i.e. 16.29
  • In last, display output on the screen of sales tax, subtotal and total.
  • Return 0 to the main function.

Also, I attach CPP file of this problem. You can download this file

Click Here to Download This File

Post a Comment

0 Comments