C++ Program to calculate profit or loss. Using user define functions.

Explanation:

  1. Pass-by-Value (calculateProfitOrLossByValue):
    • Function Definition: void calculateProfitOrLossByValue(double costPrice, double sellingPrice, double &profit, double &loss)
    • This function takes the cost price and selling price as input parameters by value.
    • It calculates profit if the selling price is greater than the cost price, otherwise, it calculates loss.
    • Profit and loss values are passed by reference to store the results.
  2. Pass-by-Reference (calculateProfitOrLossByReference):
    • Function Definition: void calculateProfitOrLossByReference(double costPrice, double sellingPrice, double &profit, double &loss)
    • This function performs the same calculations as calculateProfitOrLossByValue.
    • Profit and loss values are updated directly in the calling context.
  3. Main Function (main):
    • The program prompts the user to input the cost price and selling price.
    • It calculates and displays profit or loss using both methods.

Example Outputs:

Example 1:

Input:

  • Cost Price: 100
  • Selling Price: 120

Output:

Example 2:

Input:

  • Cost Price: 150
  • Selling Price: 130

Output:

Example 3:

Input:

  • Cost Price: 200
  • Selling Price: 200

Output: