In: Computer Science
Design a program (in C++)to calculate the stock purchasing and selling transactions.
1. ask the user to enter the name of the stock purchased, the number of share purchased, and the price per share purchased
2. assume the buyer pays 2% of the amount he paid for the stock purchase as broker commission
3. assume that the buyer sold all stocks. Ask the user to enter the price per share sold.
4. assume the buyer will pay another 2% of the amount he received for selling the stock as broker commission
the program will calculate and display:
1. the amount of money he paid for the stock purchase
2. the amount of commission he paid to the broker for purchase transaction
3. the amount he sold the stock for
4. the amount of commission he paid to the broker for purchase selling
5. the possible profit he made after selling his stock and paying the two commissions to the broker
Code:
#include <iostream>
#include<string>
int main() {
std::string stockBought;
int numberOfStocksBought;
double pricePerShareBought;
double pricePerShareSold;
double totalAmountInBuying;
double totalAmountInSelling;
// 1. ask the user to enter the name of the stock purchased, the number of share purchased, and the price per share purchased
std::cout << "Enter the name of the stock purchased: ";
std:: cin >> stockBought;
std::cout << "Enter the number of share purchased: ";
std:: cin >> numberOfStocksBought;
std::cout << "Enter the price per share bought: ";
std:: cin >> pricePerShareBought;
//2. assume the buyer pays 2% of the amount he paid for the stock purchase as broker commission; Total amount = amount spent in buying the stock + brokerage
totalAmountInBuying = (pricePerShareBought * numberOfStocksBought) +(0.02* pricePerShareBought * numberOfStocksBought) ;
//3. assume that the buyer sold all stocks. Ask the user to enter the price per share sold.
std::cout << "Enter the price per share sold: ";
std:: cin >> pricePerShareSold;
//4. assume the buyer will pay another 2% of the amount he received for selling the stock as broker commission; Total amount = amount received after selling the stock - brokerage
totalAmountInSelling = (pricePerShareSold * numberOfStocksBought)-(0.02* pricePerShareSold * numberOfStocksBought) ;
//Printing all the values
std::cout <<"The amount of money paid for the stock purchase: "<< (pricePerShareBought * numberOfStocksBought);
std::cout <<"\nThe amount of commission he paid to the broker for purchase transaction: "<< (0.02* pricePerShareBought * numberOfStocksBought);
std::cout <<"\nThe amount the stock was sold: "<<(pricePerShareSold * numberOfStocksBought);
std::cout <<"\nThe amount of commission paid to the broker for purchase selling: "<<(0.02* pricePerShareSold * numberOfStocksBought);
std::cout <<"\nThe profit made after selling his stock and paying the two commissions to the broker: "<<totalAmountInSelling - totalAmountInBuying;
}
Output:
Please do hit a like if you find my answer satisfactory!!