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 nfloat percent(int n, int x){ float p = n * x; p /= 100; return p;} // Function to return the total lossfloat 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 codeint 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 approachclass 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 ndef percent(n, x): p = (int)(n) * x; p /= 100; return p; # Function to return the total lossdef 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 codeprice = [ 20, 48, 200, 100 ];quantity = [ 20, 48, 1, 1 ];X = [ 0, 48, 200, 5 ]; # Total itemsn = 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 nfunction percent($n, $x){ $p = (int)($n) * $x; $p /= 100; return $p;} // Function to return the total lossfunction 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.


