Answer:
Here is the C program:
#include <stdio.h> //to use input output functions
int main(){ //start of main function
char *stock_name=NULL; //to store stock name (this is used instead of using char type array)
int shares; //to store number of shares
float buy_price; //to store the buy price
float cur_price; //to store the current price
float fees; //to store the fees
double initial_cost; //to store the computed initial cost
double current_cost; //to store the value of computed current cost
double profit; //to store the calculated value of profit
printf("enter stock name "); //prompts to enter stock name
scanf("%m[^\\]",&stock_name); //reads the input stock name
printf("enter number of shares "); //prompts to enter number of shares
scanf("%d", &shares); //reads the input number of shares
printf("enter the buy price, current price and fees "); //prompts to enter buy price, current price and fees values from user
scanf("%f %f %f", &buy_price, &cur_price, &fees); //reads the values of buy price, current price and fees from user
initial_cost = shares * buy_price; //computes initial cost
current_cost = shares *cur_price; //computes current cost
profit = current_cost - initial_cost - fees; //computes profit for each stock
printf("The stock name is: %s\t\\",stock_name); //displays the stock name
printf("The number of shares: \t%d\t\t\\",shares); //displays the number of shares
printf("The buy price is:\t$\t %0.2f\t\\",buy_price); //displays the buy price
printf("The current price is:\t$\t %0.2f\\",cur_price); //displays the current price
printf("The fees are:\t\t$\t %0.2f\\",fees); //displays the fees
printf("The initial cost is:\t$\t %0.2f\\",initial_cost); //displays the computed initial cost
printf("The current cost is:\t$\t %0.2f\\",current_cost); //displays the computed current cost
printf("The profit is:\t\t$\t %0.2f\\",profit);//displays the computed profit for each stock
return 0; }
Step-by-step explanation:
Lets say the user input IBM, 150, 11.33 13.33 and 5.00 as stock name, number of shares, buy price, current price and fees values.
So,
stock_name = "IBM"
shares = 150
buy_price = 11.33
cur_price = 13.33
fees = 5.00
Now initial cost is computed as:
initial_cost = shares * buy_price;
This becomes:
initial_cost = 150* 11.33
initial_cost = 1699.5
Next current cost is computed as:
current_cost = shares *cur_price;
This becomes:
current_cost = 150*13.33
current_cost = 1999.5
Next the profit is computed as:
profit = current_cost - initial_cost - fees;
This becomes:
profit = 1999.5 - 1699.5 - 5.00
profit = 295
These values are displayed on the output screen up to 2 decimal places. So the output of the entire program is:
The stock name is: IBM The number of shares: 150 The buy price is: $ 11.33 The current price is: $ 13.33 The fees are: $ 5.00 The initial cost is: $ 1699.50 The current cost is: $ 1999.50 The profit is: $ 295.00
The screenshot of the output is attached.