Friday, October 24, 2025
HomeLanguagesPython Program to find the profit or loss when CP of N...

Python Program to find the profit or loss when CP of N items is equal to SP of M items

Given N   and M   denoting that the Cost Price of N articles is equal to the Selling Price of M articles. The task is to determine the profit or Loss percentage.

Examples: 

Input:  N = 8, M = 9
Output: Loss = -11.11%

Input:  N = 8, M = 5
Output: Profit = 60%

Formula:-
 

Below is the implementation of the above approach:

Python3




# Python 3 implementation of above formula
  
# Function to calculate Profit or loss
def profitLoss(N, M):
    if (N == M):
        print("No Profit nor Loss")
    else:
        result = 0.0
  
        result = float(abs(N - M)) / M
  
        if (N - M < 0):
            print("Loss = -",'{0:.6}'
                   format(result * 100), "%")
        else:
            print("Profit = ",'{0:.6}'
                   format(result * 100), "%")
  
# Driver Code
if __name__ == '__main__':
    N = 8
    M = 9
  
    profitLoss(N, M)


Output: 

Loss = -11.1111%

 

Time Complexity: O(1)

Auxiliary Space: O(1)

Please refer complete article on Program to find the profit or loss when CP of N items is equal to SP of M items for more details!

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS