Profit and loss Problems using C

In C programming, profit and loss problems involve calculating the financial gain or loss from a transaction based on the cost price and selling price. Given a cost price (cp) and selling price (sp), we need to determine whether a profit was earned, a loss was suffered, or there was no profit nor loss.

Syntax

// Basic profit and loss calculation
if (selling_price > cost_price) {
    profit = selling_price - cost_price;
} else if (cost_price > selling_price) {
    loss = cost_price - selling_price;
} else {
    // No profit, no loss
}

Logic

The logic for profit and loss calculation is −

  • If selling price > cost price → Profit = selling price - cost price
  • If cost price > selling price → Loss = cost price - selling price
  • If selling price = cost price → No profit, no loss

Example

Here's a complete C program to calculate profit and loss −

#include <stdio.h>

// Function to calculate profit
int calculateProfit(int cp, int sp) {
    return (sp - cp);
}

// Function to calculate loss
int calculateLoss(int cp, int sp) {
    return (cp - sp);
}

int main() {
    int cp = 5000, sp = 6700;
    
    printf("Cost Price: %d<br>", cp);
    printf("Selling Price: %d<br>", sp);
    
    if (sp == cp) {
        printf("No profit nor Loss<br>");
    } else if (sp > cp) {
        printf("Profit: %d<br>", calculateProfit(cp, sp));
    } else {
        printf("Loss: %d<br>", calculateLoss(cp, sp));
    }
    
    return 0;
}
Cost Price: 5000
Selling Price: 6700
Profit: 1700

Example with Different Cases

Let's test all three scenarios −

#include <stdio.h>

void checkProfitLoss(int cp, int sp) {
    printf("Cost Price: %d, Selling Price: %d - ", cp, sp);
    
    if (sp == cp) {
        printf("No profit nor Loss<br>");
    } else if (sp > cp) {
        printf("Profit: %d<br>", sp - cp);
    } else {
        printf("Loss: %d<br>", cp - sp);
    }
}

int main() {
    // Test case 1: Profit
    checkProfitLoss(149, 229);
    
    // Test case 2: Loss  
    checkProfitLoss(149, 129);
    
    // Test case 3: No profit, no loss
    checkProfitLoss(200, 200);
    
    return 0;
}
Cost Price: 149, Selling Price: 229 - Profit: 80
Cost Price: 149, Selling Price: 129 - Loss: 20
Cost Price: 200, Selling Price: 200 - No profit nor Loss

Key Points

  • Always compare selling price with cost price to determine the transaction outcome
  • Profit is calculated as selling price - cost price
  • Loss is calculated as cost price - selling price
  • Use conditional statements to handle all three scenarios

Conclusion

Profit and loss calculations in C are straightforward using conditional statements. The key is comparing cost price and selling price to determine whether a transaction resulted in profit, loss, or break-even.

Updated on: 2026-03-15T12:58:39+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements