How to find Profit or loss using Python when CP of N items is equal to SP of M

In this article, we will learn a Python program to find the profit or loss when the cost price (CP) of N items is equal to the selling price (SP) of M items.

When CP of N items equals SP of M items, we can calculate profit or loss percentage using a mathematical relationship between these quantities.

Understanding the Problem

Given that Cost Price of N items = Selling Price of M items, we need to determine whether there's a profit or loss and calculate the percentage.

What is Cost Price (CP)?

Cost price is the amount a seller pays to purchase a product or commodity before adding profit margin.

What is Selling Price (SP)?

Selling price is the amount a customer pays to buy the product, which includes the cost price plus profit (or minus loss).

Formula

When CP of N items = SP of M items:

If N > M: Profit % = ((N - M) / M) × 100
If N < M: Loss % = ((M - N) / M) × 100  
If N = M: No profit, no loss

Algorithm

Follow these steps to calculate profit or loss ?

  • Accept N (items for cost price) and M (items for selling price) as input

  • Compare N and M values

  • If N = M, then no profit or loss

  • If N > M, calculate profit percentage

  • If N < M, calculate loss percentage

  • Display the result with appropriate formatting

Example

The following program calculates profit or loss percentage when CP of N items equals SP of M items ?

def findProfitOrLoss(n, m):
    """
    Calculate profit or loss percentage when CP of n items = SP of m items
    """
    # Check if n and m are equal
    if n == m:
        print("Neither profit nor loss!")
    else:
        # Calculate profit/loss percentage
        percentage = abs(n - m) / m * 100
        
        if n > m:
            # Profit case: CP of more items = SP of fewer items
            print(f"Profit percentage: {percentage:.2f}%")
        else:
            # Loss case: CP of fewer items = SP of more items
            print(f"Loss percentage: {percentage:.2f}%")

# Test with different values
print("Case 1: CP of 10 items = SP of 7 items")
findProfitOrLoss(10, 7)

print("\nCase 2: CP of 5 items = SP of 8 items")
findProfitOrLoss(5, 8)

print("\nCase 3: CP of 6 items = SP of 6 items")
findProfitOrLoss(6, 6)
Case 1: CP of 10 items = SP of 7 items
Profit percentage: 42.86%

Case 2: CP of 5 items = SP of 8 items
Loss percentage: 37.50%

Case 3: CP of 6 items = SP of 6 items
Neither profit nor loss!

Understanding the Logic

Let's understand why this formula works ?

# Example: CP of 10 items = SP of 7 items
# If CP of 1 item = x, then CP of 10 items = 10x
# This equals SP of 7 items = 7 × SP of 1 item
# So SP of 1 item = 10x/7

# Profit per item = SP - CP = (10x/7) - x = 3x/7
# Profit percentage = (Profit/CP) × 100 = (3x/7)/x × 100 = 3/7 × 100 = 42.86%

n, m = 10, 7
profit_percentage = (n - m) / m * 100
print(f"Manual calculation: {profit_percentage:.2f}%")
Manual calculation: 42.86%

Enhanced Version with Input Validation

def calculateProfitLoss():
    """
    Interactive function to calculate profit/loss with input validation
    """
    try:
        n = int(input("Enter number of items for Cost Price: "))
        m = int(input("Enter number of items for Selling Price: "))
        
        if n <= 0 or m <= 0:
            print("Please enter positive numbers only!")
            return
            
        if n == m:
            print("Result: No profit, no loss (Break-even)")
        else:
            percentage = abs(n - m) / m * 100
            
            if n > m:
                print(f"Result: Profit of {percentage:.2f}%")
                print(f"Explanation: Cost of {n} items = Selling price of {m} items")
            else:
                print(f"Result: Loss of {percentage:.2f}%")
                print(f"Explanation: Cost of {n} items = Selling price of {m} items")
                
    except ValueError:
        print("Please enter valid integer values!")

# Example usage (commented out for online execution)
# calculateProfitLoss()

# Direct calculation for demonstration
print("Demo calculation:")
findProfitOrLoss(12, 8)  # Should show profit
findProfitOrLoss(6, 10)  # Should show loss
Demo calculation:
Profit percentage: 50.00%
Loss percentage: 40.00%

Time and Space Complexity

  • Time Complexity: O(1) - Constant time as we perform only basic arithmetic operations

  • Space Complexity: O(1) - Constant space as we use only a few variables

Conclusion

This program efficiently calculates profit or loss percentage when the cost price of N items equals the selling price of M items using simple mathematical formulas. The solution has optimal O(1) time complexity and provides clear results with proper formatting.

Updated on: 2026-03-26T23:53:26+05:30

673 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements