- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# 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.

Assume we have taken N, and M values which represent the cost price of N items are equal to the selling price of M items. We will now calculate the profit or loss percentage.

**Formula**

profit/loss = ( (Cost Price) - (Selling Price) ) / (Selling Price) * 100

## What is the Selling Price(SP)?

The price a consumer pays to purchase a product or a commodity is known as the **selling price**. It is a price that is higher than the cost price and also includes a portion of the profit.

## What is the Cost Price(CP)?

Cost price is the cost at which the seller buys the product or the commodity. He follows that by adding a portion of gain or profits.

## What are Profit and Loss?

The amount gained from selling an item for more than its cost price is known as the **profit**.

Profit = Selling Price – Cost Price.

**Loss** is the amount lost by selling an item for less than its cost price.

Loss = Cost Price - Selling Price

### Algorithm (Steps)

Following are the Algorithms/steps to be followed to perform the desired task. −

Create a function

**findProfitOrLoss()**to calculate profit or loss percentage when the**CP**(cost price) of**'n'**items is equal to the**SP**(selling price) of**'m'**items by accepting the n, m values as arguments.Use the

**if conditional**statement to check whether the n and m values are equal with the == operator.Print

**"Neither profit nor loss!!!"**if the condition is**true**.Else calculate the profit or loss percentage.

Create a variable to store profit/loss percentage results.

Calculate the value of profit or loss by substituting the cost and selling prices to the above formula with the help of the

**abs()**function(calculates the absolute value of a number passed)If the cost price is greater than the selling price then it is the case of loss then print the loss percentage.

Else print the profit percentage.

Create a variable to store the input

**n**value.Create another variable to store the input

**m**value.Call the above-defined

**findProfitOrLoss()**function by passing n, m values to it to print the profit or loss percentage.

### Example

The following program returns the profit or loss percentage from the n, m input values using the above-given formula −

# creating a function to calculate profit or loss % # when CP of 'n' items is equal to the SP of 'm' items # by accepting the n, m values as arguments def findProfitOrLoss(n, m): # checking whether the value of n, m are equal if (n == m): # printing "Neither profit nor loss!!!" if the condition is true print("Neither profit nor loss!!!") else: # variable to store profit/loss result output = 0.0 # Calculating value of profit/loss output = float(abs(n - m)) / m # checking whether n-m value value is less than 0 if (n - m < 0): # printing the loss percentage upto 4 digits after decimals print("The Loss percentage is: -", '{0:.4}' .format(output * 100), "%") else: # printing the profit percentage upto 4 digits after decimals print("The Profit percentage is: ", '{0:.6}' . format(output * 100), "%") # input n value n = 10 # input m value m = 7 # calling the above defined findProfitOrLoss() function # by passing n, m values to it to print the profit or loss percentage findProfitOrLoss(n, m)

### Output

On execution, the above program will generate the following output −

The Profit percentage is: 42.8571 %

**Time Complexity** − O(1)

**Auxiliary Space** − O(1)

We substituted the numbers in the formula such that there are no loops to traverse, hence it just takes linear time, i.e. O(1) Time Complexity.

## Conclusion

In this article, we learned how to use Python to calculate profit or loss when the cost price of N items equals the selling price of M items. This solution was implemented using a linear time complexity approach. We also learned how to use the format() function to format the float integer to n digits.