C program to calculate profit or loss


Efficiently calculate profit or loss in C programming using cost price and selling price inputs. Explore how to analyze basic financial scenarios and determine profit or loss outcomes.

Understanding profits or losses is fundamental in financial analysis. In this article, we'll explore how to create a simple C program to calculate profit or loss based on cost price and selling price.

Understanding Profit and Loss

  • Profit: When the selling price (SP) of a product exceeds the cost price (CP), the result is a profit.
  • Loss: If the CP is greater than the SP, the outcome is a loss.

C program to calculate profit or loss

Let's delve into the C programming language to create a program that calculates profit or loss based on user input for CP and SP.

#include <stdio.h>

int main() {
    float cp, sp, profit, loss;

    // Input from the user
    printf("Enter the Cost Price: ");
    scanf("%f", &cp);

    printf("Enter the Selling Price: ");
    scanf("%f", &sp);

    // Calculating profit or loss
    if (sp > cp) {
        profit = sp - cp;
        printf("Profit: %.2f", profit);
    } else if (cp > sp) {
        loss = cp - sp;
        printf("Loss: %.2f", loss);
    } else {
        printf("No Profit, No Loss");
    }

    return 0;
}

Output

Enter the Cost Price: 1500
Enter the Selling Price: 2000
Profit: 500.00

Enter the Cost Price: 3000
Enter the Selling Price: 2500
Loss: 500.00

Enter the Cost Price: 2000
Enter the Selling Price: 2000
No Profit, No Loss

Understanding the Code

  1. User Input: The user is prompted to enter the CP and SP of a product, which are stored in variables cp and sp using scanf().
  2. Profit or Loss Calculation: The program checks if there's a profit or loss by comparing the values of CP and SP. It then calculates and displays the profit or loss accordingly.

In this article, we've created a simple C program that efficiently calculates profit or loss based on the given cost price and selling price. Understanding the concept of profit and loss and implementing conditional checks through C programming enables us to analyse basic financial scenarios.


Recommended Posts