- Write a C program to enter cost price and selling price and find profit or loss using if else statement.
Required Knowledge
- C printf and scanf functions
- If Else statement in C
- If Selling Price > Cost Price
- Profit = Selling Price – Cost Price
- If Selling Price < Cost Price
- Loss = Cost Price – Selling Price
- If Selling Price = Cost Price
- No Profit .. No Loss
C program to find profit and loss, given cost price and selling price

/*
* Given cost price and selling price, Write a
* C program to calculate Profit or loss
*/
#include <stdio.h>
int main() {
int costPrice, sellingPrice;
/*
* Take costPrice and SellingPrice as input from user
*/
printf("Enter Cost Price and Selling Price\n");
scanf("%d %d", &costPrice, &sellingPrice);
if(costPrice > sellingPrice) {
/* Loss */
printf("Loss = %d\n", costPrice - sellingPrice);
} else if(sellingPrice > costPrice) {
/* Profit or Gain*/
printf("Profit = %d\n", sellingPrice - costPrice);
} else {
/* No Profit or Loss*/
printf("No Profit and No Loss\n");
}
return 0;
}
Output
Enter Cost Price and Selling Price 5 10 Profit = 5
Enter Cost Price and Selling Price 12 8 Loss = 4
Enter Cost Price and Selling Price 10 10 No Profit and No Loss