Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Write a C program to find out profit or loss in buying an article
In business transactions, calculating profit or loss is essential to determine the financial outcome. Profit occurs when the selling price exceeds the cost price, while loss occurs when the cost price is greater than the selling price.
Syntax
if (selling_price > cost_price) {
profit = selling_price - cost_price;
} else if (cost_price > selling_price) {
loss = cost_price - selling_price;
} else {
// No profit, no loss
}
Formulas
The formulas for calculating profit and loss are −
-
Profit: When Selling Price > Cost Price
profit = selling_price - cost_price -
Loss: When Cost Price > Selling Price
loss = cost_price - selling_price - No Profit No Loss: When Selling Price = Cost Price
Example
Following is the C program to find the profit or loss −
#include <stdio.h>
int main() {
float costPrice, sellingPrice, amount;
printf("Enter the product cost: ");
scanf("%f", &costPrice);
printf("Enter the selling price: ");
scanf("%f", &sellingPrice);
if (sellingPrice > costPrice) {
amount = sellingPrice - costPrice;
printf("Profit Amount = %.2f<br>", amount);
}
else if (costPrice > sellingPrice) {
amount = costPrice - sellingPrice;
printf("Loss Amount = %.2f<br>", amount);
}
else {
printf("No Profit No Loss!<br>");
}
return 0;
}
Output
Enter the product cost: 450 Enter the selling price: 475.8 Profit Amount = 25.80
How It Works
The program uses conditional statements to determine the outcome −
- Read the cost price and selling price from the user
- Compare selling price with cost price using if-else conditions
- Calculate and display profit, loss, or break-even status accordingly
Conclusion
This program effectively calculates profit or loss by comparing cost and selling prices using simple arithmetic operations and conditional statements. It handles all three scenarios: profit, loss, and break-even situations.
Advertisements
