Program to calculate Profit Or Loss

07 Jul, 2022
Given the Cost Price(CP) and Selling Price(SP) of a product. The task is to Calculate the Profit or Loss.Examples:  
Input: CP = 1500, SP = 2000
Output: 500 Profit

Input: CP = 3125, SP = 1125
Output: 2000 Loss
Formula:  
Profit = (Selling Price - Cost Price)
Loss = (Cost Price - Selling Price)
 
Below is the required implementation:  
C++
Java
Python 3
C#
PHP
Javascript
// C++ code to demonstrate Profit and Loss#include <iostream>using namespace std; // Function to calculate Profit.int Profit(int costPrice, int sellingPrice){    int profit = (sellingPrice - costPrice);     return profit;} // Function to calculate Loss.int Loss(int costPrice, int sellingPrice){    int Loss = (costPrice - sellingPrice);     return Loss;} // Driver Code.int main(){    int costPrice = 1500, sellingPrice = 2000;     if (sellingPrice == costPrice)        cout << "No profit nor Loss";     else if (sellingPrice > costPrice)        cout << Profit(costPrice, sellingPrice) << " Profit ";     else        cout << Loss(costPrice, sellingPrice) << " Loss ";     return 0;}
// Java code to demonstrate// Profit and Lossclass GFG{// Function to calculate Profit.static int Profit(int costPrice,                  int sellingPrice){    int profit = (sellingPrice - costPrice);     return profit;} // Function to calculate Loss.static int Loss(int costPrice,                int sellingPrice){    int Loss = (costPrice - sellingPrice);     return Loss;} // Driver Code.public static void main(String[] args){    int costPrice = 1500,        sellingPrice = 2000;     if (sellingPrice == costPrice)        System.out.println("No profit nor Loss");     else if (sellingPrice > costPrice)        System.out.println(Profit(costPrice,                                  sellingPrice) +                                     " Profit ");     else        System.out.println(Loss(costPrice,                                sellingPrice) +                                     " Loss ");}} // This code is contributed// by ChitraNayal
# Python 3 program to demonstrate# Profit and Loss # Function to calculate Profit.def Profit(costPrice, sellingPrice) :     profit = (sellingPrice - costPrice)     return profit # Function to calculate Loss.def Loss(costPrice, sellingPrice) :     Loss = (costPrice - sellingPrice)     return Loss # Driver codeif __name__ == "__main__" :     costPrice, sellingPrice = 1500, 2000     if sellingPrice == costPrice :        print("No profit nor Loss")     elif sellingPrice > costPrice :        print(Profit(costPrice,                     sellingPrice), "Profit")     else :        print(Loss(costPrice,                   sellingPrice), "Loss") # This code is contributed by ANKITRAI1
// C# code to demonstrate Profit and Lossusing System;class GFG{// Function to calculate Profitstatic int Profit(int costPrice,                  int sellingPrice){    int profit = (sellingPrice - costPrice);     return profit;} // Function to calculate Lossstatic int Loss(int costPrice,                int sellingPrice){    int Loss = (costPrice - sellingPrice);     return Loss;} // Driver Codepublic static void Main(){    int costPrice = 1500,        sellingPrice = 2000;     if (sellingPrice == costPrice)        Console.Write("No profit nor Loss");     else if (sellingPrice > costPrice)        Console.Write(Profit(costPrice,                      sellingPrice) + " Profit ");     else        Console.Write(Loss(costPrice,                      sellingPrice) + " Loss ");}} // This code is contributed by ChitraNayal
<?php// PHP code to demonstrate// Profit and Loss // Function to calculate Profit.function Profit($costPrice,                $sellingPrice){    $profit = ($sellingPrice -               $costPrice);     return $profit;} // Function to calculate Loss.function Loss($costPrice,              $sellingPrice){    $Loss = ($costPrice -             $sellingPrice);     return $Loss;} // Driver Code.$costPrice = 1500;$sellingPrice = 2000; if ($sellingPrice == $costPrice)    echo "No profit nor Loss"; else if ($sellingPrice > $costPrice)    echo Profit($costPrice,                $sellingPrice)." Profit "; else    echo Loss($costPrice,              $sellingPrice)." Loss "; // This code is contributed// by ChitraNayal?>
<script>    // Javascript code to demonstrate Profit and Loss          // Function to calculate Profit.    function Profit(costPrice, sellingPrice)    {        let profit = (sellingPrice - costPrice);         return profit;    }     // Function to calculate Loss.    function Loss(costPrice, ellingPrice)    {        let Loss = (costPrice - sellingPrice);         return Loss;    }         let costPrice = 1500, sellingPrice = 2000;       if (sellingPrice == costPrice)        document.write("No profit nor Loss");       else if (sellingPrice > costPrice)        document.write(Profit(costPrice, sellingPrice) + " Profit ");       else        document.write(Loss(costPrice, sellingPrice) + " Loss "); // This code is contributed by divyeshrabadiya07.</script>
OUTPUT :  
500 Profit
Time Complexity: O(1)Auxiliary Space: O(1) 
ankthon
ukasp
divyeshrabadiya07
hasani
school-programming
Mathematical
School Programming
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Merge two sorted arrays
Operators in C / C++
Prime Numbers
Minimum number of jumps to reach end
Find minimum number of coins that make a given value
Python Dictionary
Reverse a string in Java
Arrays in C/C++
Introduction To PYTHON
Interfaces in Java