C++ LANGUAGE!!!! Write a program to calculate stock transaction. The program wil
ID: 3791338 • Letter: C
Question
C++ LANGUAGE!!!!
Write a program to calculate stock transaction. The program will ask the user to enter the follow information:
The name of the share purchased
The number of shares purchased
The price per share purchased
the price per share sold
In this question, we assume the user sold all the shares purchased. The user will pay the stockbroker a commission that amounted to 2% of the amount he/she paid for the stock when the user purchased the stock. The user will pay the stockbroker another commission that also amounted to 2% of the amount he/she paid for the stock when the user sold the stock.
Then the program will calculate the following:
the amount of money paid for the stock for the stock purchase
the amount of commission paid to the broker for stock purchase
the total amount of money paid for the stock purchase transaction
the amount of money sold for the stock
the amount of commission paid to the broker for stock sale
the total amount of money the user got for stock sale after paying the commission
the net profit after selling the stock and paying two commissions to the broker
Explanation / Answer
// C++ code
#include <iostream>
#include <string>
#include <fstream>
#include <stdlib.h>
#include <math.h>
#include <vector>
#include <iomanip> // std::setprecision
using namespace std;
int main()
{
double brokersCommission = 0.02; // Broker commission rate
string name;
double number, pricePurchased, priceSold;
cout << "Enter name of the share purchased: ";
getline(cin , name);
cout << "Enter number of shares purchased: ";
cin >> number;
cout << "Enter price per share purchased: ";
cin >> pricePurchased;
cout << "Enter price per share sold: ";
cin >> priceSold;
double purchaseAmount = number * pricePurchased;
double purchaseCommission = purchaseAmount * brokersCommission ;
double totalAmountPaid = purchaseAmount + purchaseCommission;
double stockSaleAmount = number * priceSold;
double sellingCommission = (number * priceSold) * brokersCommission;
double totalAmountRecieved = stockSaleAmount - sellingCommission;
double netProfitLoss = totalAmountRecieved - totalAmountPaid;
cout << " The amount of money paid for the stock for the stock purchase: " << purchaseAmount << endl;
cout << "The amount of commission paid to the broker for stock purchase: " << purchaseCommission << endl;
cout << "The total amount of money paid for the stock purchase transaction: " << totalAmountPaid << endl;
cout << "The amount of money sold for the stock: " << stockSaleAmount << endl;
cout << "The amount of commission paid to the broker for stock sale: " << sellingCommission << endl;
cout << "The total amount of money you got for stock sale after paying the commission: " << totalAmountRecieved << endl;
cout << "The net profit after selling the stock and paying two commissions to the broker: " << netProfitLoss << endl;
return 0;
}
/*
output:
Enter name of the share purchased: Nifty
Enter number of shares purchased: 1000
Enter price per share purchased: 32.87
Enter price per share sold: 33.92
The amount of money paid for the stock for the stock purchase: 32870
The amount of commission paid to the broker for stock purchase: 657.4
The total amount of money paid for the stock purchase transaction: 33527.4
The amount of money sold for the stock: 33920
The amount of commission paid to the broker for stock sale: 678.4
The total amount of money you got for stock sale after paying the commission: 33241.6
The net profit after selling the stock and paying two commissions to the broker: -285.8
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.