Write a C program to calculate the profit or loss on a product

Description: Take the cost price and selling price of a product. Print the profit or the loss amount. If both are the same, print “No profit, no loss”.

Hint:

  • Profit = selling price – cost price (when selling price is more). Loss = cost price – selling price.

C program to calculate profit or loss

#include <stdio.h>

int main(void) {
    float cost, sell;

    printf("Enter cost price and selling price: ");
    scanf("%f %f", &cost, &sell);

    if (sell > cost) {
        printf("Profit = %.2f\n", sell - cost);
    } else if (cost > sell) {
        printf("Loss = %.2f\n", cost - sell);
    } else {
        printf("No profit, no loss\n");
    }
    return 0;
}

Output:

Output of the above program is

Enter cost price and selling price: 500 650
Profit = 150.00

How it works

  • If the selling price is more than the cost price, the difference is profit.
  • If it is less, the difference is loss. Otherwise there is neither.

Read Else If Ladder.


Learn more about the similar topics:
Tutorials
No Content Found.
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.