Skip to content
Related Articles

Related Articles

Calculate the loss incurred in selling the given items at discounted price
  • Last Updated : 29 Apr, 2019

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 = 300 – 288 = 12 (for a single item)
For 7 items, 12 * 7 = 84

Input: price[] = {20, 48, 200, 100}, quantity[] = {20, 48, 1, 1}, X[] = {0, 48, 200, 5}
Output: 1330.17

Approach: For every item, calculate its selling price i.e. original price + X% of the original price then calculate the discounted price as selling price – X% of the selling price. Now, loss can be calculated as (original price – discounted price) * quantity. Add the loss incurred for all the items which is the required answer.



Below is the implementation of the above approach:

C++




// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to return the x% of n
float percent(int n, int x)
{
    float p = n * x;
    p /= 100;
    return p;
}
  
// Function to return the total loss
float getLoss(int price[], int quantity[], int X[], int n)
{
    // To store the total loss
    float loss = 0;
  
    for (int i = 0; i < n; i++) {
  
        // Original price of the item
        float originalPrice = price[i];
  
        // The price at which the item will be sold
        float sellingPrice = originalPrice
                             + percent(originalPrice, X[i]);
  
        // The discounted price of the item
        float afterDiscount = sellingPrice
                              - percent(sellingPrice, X[i]);
  
        // Loss incurred
        loss += ((originalPrice - afterDiscount) * quantity[i]);
    }
  
    return loss;
}
  
// Driver code
int main()
{
    int price[] = { 20, 48, 200, 100 };
    int quantity[] = { 20, 48, 1, 1 };
    int X[] = { 0, 48, 200, 5 };
  
    // Total items
    int n = sizeof(X) / sizeof(X[0]);
    cout << getLoss(price, quantity, X, n);
  
    return 0;
}

Java




// Java implementation of the approach
class GFG {
    // Function to return the x% of n
    static float percent(int n, int x)
    {
        float p = n * x;
        p /= 100;
        return p;
    }
  
    // Function to return the total loss
    static float getLoss(int price[], int quantity[], int X[], int n)
    {
        // To store the total loss
        float loss = 0;
  
        for (int i = 0; i < n; i++) {
  
            // Original price of the item
            float originalPrice = price[i];
  
            // The price at which the item will be sold
            float sellingPrice = originalPrice
                                 + percent((int)originalPrice, X[i]);
  
            // The discounted price of the item
            float afterDiscount = sellingPrice
                                  - percent((int)sellingPrice, X[i]);
  
            // Loss incurred
            loss += ((originalPrice - afterDiscount) * quantity[i]);
        }
  
        return loss;
    }
  
    // Driver code
    public static void main(String args[])
    {
        int price[] = { 20, 48, 200, 100 };
        int quantity[] = { 20, 48, 1, 1 };
        int X[] = { 0, 48, 200, 5 };
  
        // Total items
        int n = X.length;
        System.out.print(getLoss(price, quantity, X, n));
    }
}

Python3




# Python3 implementation of the approach
  
# Function to return the x% of n
def percent(n, x):
  
    p = (int)(n) * x;
    p /= 100;
    return p;
  
# Function to return the total loss
def getLoss(price, quantity, X, n):
  
    # To store the total loss
    loss = 0;
  
    for i in range(n):
  
        # Original price of the item
        originalPrice = price[i];
  
        # The price at which the item will be sold
        sellingPrice = originalPrice + percent(originalPrice, X[i]);
  
        # The discounted price of the item
        afterDiscount = sellingPrice - percent(sellingPrice, X[i]);
  
        # Loss incurred
        loss += ((originalPrice - afterDiscount) * quantity[i]);
  
    return round(loss,2);
  
# Driver code
price = [ 20, 48, 200, 100 ];
quantity = [ 20, 48, 1, 1 ];
X = [ 0, 48, 200, 5 ];
  
# Total items
n = len(X);
print(getLoss(price, quantity, X, n));
  
      
# This code is contributed by mits

C#




// C# implementation of the approach 
using System;
  
class GFG 
      
// Function to return the x% of n 
static float percent(int n, int x) 
    float p = n * x; 
    p /= 100; 
    return p; 
  
// Function to return the total loss 
static float getLoss(int []price, 
                     int []quantity, 
                     int []X, int n) 
    // To store the total loss 
    float loss = 0; 
  
    for (int i = 0; i < n; i++)
    
  
        // Original price of the item 
        float originalPrice = price[i]; 
  
        // The price at which the item will be sold 
        float sellingPrice = originalPrice + 
                percent((int)originalPrice, X[i]); 
  
        // The discounted price of the item 
        float afterDiscount = sellingPrice - 
                 percent((int)sellingPrice, X[i]); 
  
        // Loss incurred 
        loss += ((originalPrice - 
                  afterDiscount) * quantity[i]); 
    
  
    return loss; 
  
// Driver code 
public static void Main() 
    int []price = { 20, 48, 200, 100 }; 
    int []quantity = { 20, 48, 1, 1 }; 
    int []X = { 0, 48, 200, 5 }; 
  
    // Total items 
    int n = X.Length; 
    Console.Write(getLoss(price, quantity, X, n)); 
}
  
// This code is contributed by Ryuga

PHP




<?php
// PHP implementation of the approach
  
// Function to return the x% of n
function percent($n, $x)
{
    $p = (int)($n) * $x;
    $p /= 100;
    return $p;
}
  
// Function to return the total loss
function getLoss($price, $quantity, $X,$n)
{
    // To store the total loss
    $loss = 0;
  
    for ($i = 0; $i < $n; $i++) 
    {
  
        // Original price of the item
        $originalPrice = $price[$i];
  
        // The price at which the item will be sold
        $sellingPrice = $originalPrice
                            + percent($originalPrice, $X[$i]);
  
        // The discounted price of the item
        $afterDiscount = $sellingPrice
                            - percent($sellingPrice, $X[$i]);
  
        // Loss incurred
        $loss += (($originalPrice -
                    $afterDiscount) * $quantity[$i]);
    }
  
    return $loss;
}
  
    // Driver code
    $price = array( 20, 48, 200, 100 );
    $quantity = array( 20, 48, 1, 1 );
    $X = array( 0, 48, 200, 5 );
  
    // Total items
    $n = count($X);
    echo getLoss($price, $quantity, $X, $n);
  
// This code is contributed by mits
?>
Output:
1330.17

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :