Write a C program to calculate profit or loss.

Topics

  1. The program uses the stdio.h library to enable input and output operations.
  2. The main function is defined with an integer return type, as is customary in C programs.
  3. 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.
  4. The user is prompted to enter the cost price and selling price using printf and scanf statements.
  5. An if-else statement is used to check whether the selling price is greater than or less than the cost price.
  6. 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.
  7. 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.
  8. 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.
  9. 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

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *