1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
#include <iostream> using namespace std; // Function to calculate profit or loss using pass-by-value void calculateProfitOrLossByValue(double costPrice, double sellingPrice, double &profit, double &loss) { if (sellingPrice > costPrice) { profit = sellingPrice - costPrice; loss = 0.0; } else if (costPrice > sellingPrice) { profit = 0.0; loss = costPrice - sellingPrice; } else { profit = 0.0; loss = 0.0; } } // Function to calculate profit or loss using pass-by-reference void calculateProfitOrLossByReference(double costPrice, double sellingPrice, double &profit, double &loss) { if (sellingPrice > costPrice) { profit = sellingPrice - costPrice; loss = 0.0; } else if (costPrice > sellingPrice) { profit = 0.0; loss = costPrice - sellingPrice; } else { profit = 0.0; loss = 0.0; } } int main() { double costPrice, sellingPrice; double profitByValue, lossByValue; double profitByRef, lossByRef; // Input the cost price and selling price cout << "Enter cost price: "; cin >> costPrice; cout << "Enter selling price: "; cin >> sellingPrice; // Calculate profit or loss using pass-by-value calculateProfitOrLossByValue(costPrice, sellingPrice, profitByValue, lossByValue); cout << "Using pass-by-value: "; if (profitByValue > 0) { cout << "Profit: " << profitByValue << endl; } else if (lossByValue > 0) { cout << "Loss: " << lossByValue << endl; } else { cout << "No profit, no loss." << endl; } // Calculate profit or loss using pass-by-reference calculateProfitOrLossByReference(costPrice, sellingPrice, profitByRef, lossByRef); cout << "Using pass-by-reference: "; if (profitByRef > 0) { cout << "Profit: " << profitByRef << endl; } else if (lossByRef > 0) { cout << "Loss: " << lossByRef << endl; } else { cout << "No profit, no loss." << endl; } return 0; } |
Explanation:
- 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.
- Function Definition:
- 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.
- Function Definition:
- 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:
1 2 3 4 |
Enter cost price: 100 Enter selling price: 120 Using pass-by-value: Profit: 20 Using pass-by-reference: Profit: 20 |
Example 2:
Input:
- Cost Price:
150
- Selling Price:
130
Output:
1 2 3 4 |
Enter cost price: 150 Enter selling price: 130 Using pass-by-value: Loss: 20 Using pass-by-reference: Loss: 20 |
Example 3:
Input:
- Cost Price:
200
- Selling Price:
200
Output:
1 2 3 4 |
Enter cost price: 200 Enter selling price: 200 Using pass-by-value: No profit, no loss. Using pass-by-reference: No profit, no loss. |