Howdy readers, today you will learn how to write a C++ program to calculate profit or loss.
Profit and Loss are the terms used to identify whether a deal is profitable or not. Before calculating profit or loss, you should be aware of the following terms:
- Cost Price – The price at which an article is purchased is called its cost price.
- Selling Price – The price at which an article is sold is known as the selling price of the article.
We will use the following formulas to calculate the profit and loss of a product:
- Profit = Selling Price – Cost Price, if Selling Price > Cost Price.
- Loss = Cost Price – Selling Price, if Cost Price > Selling Price
If Cost Price == Selling Price, then there is neither profit nor loss.
So, without any delay, let’s begin this tutorial.
C++ Program to Calculate Profit or Loss
C++ Program
// C++ Program to Calculate Profit or Loss #include <iostream> using namespace std; int main(){ float CP, SP, amount; // Taking input cout << "Enter the cost price of the product: "; cin >> CP; cout << "Enter the selling price of the product: "; cin >> SP; // Finding profit or loss if (SP > CP){ amount = SP - CP; cout << "Profit amount: " << amount << endl; } else if (CP > SP){ amount = CP - SP; cout << "Loss amount: " << amount << endl; } else{ cout << "No profit nor loss!!" << endl; } return 0; }
Output 1
Enter the cost price of the product: 35 Enter the selling price of the product: 42 Profit amount: 7
Output 2
Enter the cost price of the product: 70 Enter the selling price of the product: 65 Loss amount: 5
Output 3
Enter the cost price of the product: 40 Enter the selling price of the product: 40 No profit nor loss!!

Explanation
float CP, SP, amount;
In this program, we have declared three float data type variables named CP
, SP
and amount
.
cout << "Enter the cost price of the product: "; cin >> CP; cout << "Enter the selling price of the product: "; cin >> SP;
Then, this program prompts the user to enter the cost price and selling price of the price. The input gets stored in the CP
and SP
named variables respectively.
if (SP > CP){ amount = SP - CP; cout << "Profit amount: " << amount << endl; }
If the selling price of the product is greater than the cost price, then there is a profit. Profit is calculated using the mathematical formula: Profit = Selling Price – Cost Price.
amount = CP - SP; cout << "Loss amount: " << amount << endl; }
Similarly, if the cost price of the product is greater than the selling price, then there is a loss. Loss is calculated using the formula: Loss = Cost Price – Selling Price.
else{ cout << "No profit nor loss!!" << endl; }
If neither of the above two statements are true, then it clearly means the cost price of the product is equal to the selling price of the product.
In that case, there is neither profit nor loss.
Conclusion
Today you learned how to write a C++ program to calculate profit or loss.
If you have any queries regarding the tutorial, comment down your queries in the comment section.
Thanks for reading.
Happy Coding!!
Also Read:
- C++ Program to Print 1 to 100
- C++ Program to Calculate Simple Interest
- C++ Program to Calculate Compound Interest
- C++ Program to Print Odd Numbers From 1 to 100
- C++ Program to Check If a Number is Divisible by 5 and 11