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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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));
    }
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

1330.17


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : AnkitRai01, Mithun Kumar



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.