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 = 84Input: 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 ?> |
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.
Recommended Posts:
- Find cost price from given selling price and profit or loss percentage
- Loss when two items are sold at same price and same percentage profit/loss
- Program to find the profit or loss when CP of N items is equal to SP of M items
- Find Selling Price from given Profit Percentage and Cost
- Minimum, maximum and average price values for all the items of given type
- Check if two items can be selected from two different categories without exceeding price
- Maximum profit by selling N items at two markets
- Minimum total cost incurred to reach the last station
- Program to calculate Profit Or Loss
- Get maximum items when other items of total cost of an item are free
- Probability of distributing M items among X bags such that first bag contains N items
- Maximum profit by buying and selling a share at most k times
- Puzzle | Selling Chocolates
- Maximum profit after buying and selling the stocks
- Maximize the profit by selling at-most M products
- Find the maximum amount that can be collected by selling movie tickets
- Maximize the profit after selling the tickets
- Maximum profit by buying and selling a share at most K times | Greedy Approach
- Maximize profit in buying and selling stocks with Rest condition
- Maximum profit by buying and selling a stock at most twice | Set 2
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.

