Programming Challenge Chapter 3 – Q #21 Stock Transaction Program – Tony Gaddis – Starting Out With C++

Programming Challenge Chapter 3 – Q #21 Stock Transaction Program – Tony Gaddis – Starting Out With C++


Problem: -

Last month Joe purchased some stock in Acme Software, Inc. Here are the details of the purchase:
  • The number of shares that Joe purchased was 1,000. 
  • When Joe purchased the stock, he paid $32.87 per share. 
  • Joe paid his stock broker a commission that amounted to 2% of the amount he paid for the stock.
Two weeks later Joe sold the stock. Here are the details of the sale:
  • The number of shares that Joe sold was 1,000.
  • He sold the stock for $33.92 per share. 
  • He paid his stock broker another commission that amounted to 2% of the amount he received for the stock.
Write a program that displays the following information:
  • The amount of money Joe paid for the stock. 
  • The amount of commission Joe paid his broker when he bought the stock.
  • The amount that Joe sold the stock for.
  • The amount of commission Joe paid his broker when he sold the stock. 
  • Display the amount of profit that Joe made after selling his stock and paying the two commissions to his broker. (If the amount of profit that your program displays is a negative number, then Joe lost money on the transaction.)

Solution: -

#include <iostream>

#include <iomanip>

using namespace std;

int main()

{

      const double paid_stock = 32.87,

            sale_stock = 33.91,

            broker_commission = 0.02;

      const int shares = 1000;

      double price_paid_stock = 0.0, price_sale_stock = 0.0;

      double commission_sale_stock, commission_paid_stock = 0.0, profit = 0.0;

      //Calculation

      price_paid_stock = paid_stock * shares;

      price_sale_stock = sale_stock * shares;

      commission_sale_stock = broker_commission * sale_stock;

      commission_paid_stock = broker_commission * paid_stock;

      profit = price_sale_stock - price_paid_stock - commission_paid_stock - commission_sale_stock;

      cout << setprecision(2) << showpoint << fixed;

      cout << "\nThe amount of money Joe paid for the stock:             $" << price_paid_stock << endl;

      cout << "Commission Joe paid his broker when he bought the stock:  $" << commission_paid_stock << endl;

      cout << "The amount that Joe sold stock:                           $" << price_sale_stock << endl;

      cout << "Commission Joe paid his broker when he sold the stock:    $" << commission_sale_stock << endl;

      cout << "Ammount of Profit After Paying Both Commission:           $" << profit << endl;

      return 0;

}

This is the solution of this question




OUTPUT OF THIS QUESTION 


Explanation of this Solution

  1. Add a header file for math and pattern.
  2. Declare three double constants and initialize given values.
  3. Declare a int constants and initialize given values
  4. Declare 4 double variables for calculation.
  5. Calculate values according to question
  6.  In last, display output on the screen in a pattern.
  7. 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