- The program uses the
stdio.h
library to enable input and output operations. - The main function is defined with an integer return type, as is customary in C programs.
- The variables
costPrice
, sellingPrice
, profit
, and loss
are declared as float data types to store the values entered by the user and the calculated profit or loss. - The user is prompted to enter the cost price and selling price using
printf
and scanf
statements. - An
if-else
statement is used to check whether the selling price is greater than or less than the cost price. - If the selling price is greater than the cost price, the program calculates the profit by subtracting the cost price from the selling price and stores the result in the
profit
variable. - If the selling price is less than the cost price, the program calculates the loss by subtracting the selling price from the cost price and stores the result in the
loss
variable. - If the selling price is equal to the cost price, the program displays a message using the
printf
statement indicating that there is no profit or loss. - The program returns an integer value of 0, indicating successful execution.
#include <stdio.h>
int main() {
float costPrice, sellingPrice, profit, loss;
printf("Enter the cost price: ");
scanf("%f", &costPrice);
printf("Enter the selling price: ");
scanf("%f", &sellingPrice);
if (sellingPrice > costPrice) {
profit = sellingPrice - costPrice;
printf("Profit = %.2f", profit);
}
else if (sellingPrice < costPrice) {
loss = costPrice - sellingPrice;
printf("Loss = %.2f", loss);
}
else {
printf("No profit, no loss.");
}
return 0;
}
OUTPUT
Enter the cost price: 100
Enter the selling price: 120
Profit = 20.00