Open In App

Program to calculate Profit Or Loss

Last Updated : 07 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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++




// 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




// Java code to demonstrate
// Profit and Loss
class 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




# 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 code
if __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#




// C# code to demonstrate Profit and Loss
using System;
class 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()
{
    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
// 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
?>


Javascript




<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)
 



Similar Reads

Loss when two items are sold at same price and same percentage profit/loss
Given the Selling price i.e 'SP' of the two items each. One item is sold at 'P%' Profit and other at 'P%' Loss. The task is to find out the overall Loss.Examples: Input: SP = 2400, P = 30% Output: Loss = 474.725 Input: SP = 5000, P = 10% Output: Loss = 101.01 Approach: How does the above formula work? For profit making item : With selling price (10
4 min read
Program to find the profit or loss when CP of N items is equal to SP of M items
Given [Tex]N [/Tex]and [Tex]M [/Tex]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: C/C++ Code // C
4 min read
Find cost price from given selling price and profit or loss percentage
Given the Selling Price(SP) and percentage profit or loss of a product. The task is to Calculate the cost price(CP) of the product.Examples: Input: SP = 1020, Profit Percentage = 20 Output: CP = 850 Input: SP = 900, Loss Percentage = 10 Output: CP = 1000 Approach: Formula to calculate cost price if selling price and profit percentage are given: CP
5 min read
Maximize profit possible by selling M products such that profit of a product is the number of products left of that supplier
Given an array arr[] consisting of N positive integers, such that arr[i] represents the number of products the ith supplier has and a positive integer, M, the task is to find the maximum profit by selling M products if the profit of a particular product is the same as the number of products left of that supplier. Examples: Input: arr[] = {4, 6}, M
7 min read
Program to calculate the profit sharing ratio
Given an array of amounts and time_period that represents the amount of money N persons invest and the time period for which they had invested. The task is to calculate the Profit ratio at the End.Examples: Input: n = 2, Amount1 = 7000, Time1 = 12 months Amount2 = 6000, Time2 = 6 months Output: 7 : 3Input: n = 3, Amount1 = 5000, Time1 = 6 months Am
7 min read
Calculate the loss incurred in selling the given items at discounted price
A seller wants to sell his items at a discount of X%. He increases the price of each item by X% of the original price. The task is to calculate the total loss incurred after selling all the items.Examples: Input: price[] = {300}, quantity[] = {7}, X[] = {20} Output: 84.0 Original price = 300 Selling price = 360 Discounted price = 288 Loss incurred
7 min read
C Program For Stock Buy Sell To Maximize Profit
Efficient approach: If we are allowed to buy and sell only once, then we can use following algorithm. Maximum difference between two elements. Here we are allowed to buy and sell multiple times. Following is the algorithm for this problem. Find the local minima and store it as starting index. If not exists, return.Find the local maxima. and store i
3 min read
Python Program For Stock Buy Sell To Maximize Profit
The cost of a stock on each day is given in an array, find the max profit that you can make by buying and selling in those days. For example, if the given array is {100, 180, 260, 310, 40, 535, 695}, the maximum profit can earned by buying on day 0, selling on day 3. Again buy on day 4 and sell on day 6. If the given array of prices is sorted in de
5 min read
Javascript Program For Stock Buy Sell To Maximize Profit
The cost of a stock on each day is given in an array, find the max profit that you can make by buying and selling in those days. For example, if the given array is {100, 180, 260, 310, 40, 535, 695}, the maximum profit can earned by buying on day 0, selling on day 3. Again buy on day 4 and sell on day 6. If the given array of prices is sorted in de
4 min read
C++ Program For Stock Buy Sell To Maximize Profit
The cost of a stock on each day is given in an array, find the max profit that you can make by buying and selling in those days. For example, if the given array is {100, 180, 260, 310, 40, 535, 695}, the maximum profit can earned by buying on day 0, selling on day 3. Again buy on day 4 and sell on day 6. If the given array of prices is sorted in de
5 min read