Find cost price from given selling price and profit or loss percentage in C++

C++Server Side ProgrammingProgramming

Consider we have the selling price, and percentage of profit or loss is given. We have to find the cost price of the product. The formula is like below −

$$Cost \: Price = \frac{Sell Price * 100}{100 + Percentage \: Profit}$$

$$Cost \: Price = \frac{Sell price *100}{100 + percentage\:loss}$$

Example

 Live Demo

#include<iostream>
using namespace std;
float priceWhenProfit(int sellPrice, int profit) {
   return (sellPrice * 100.0) / (100 + profit);
}
float priceWhenLoss(int sellPrice, int loss) {
   return (sellPrice * 100.0) / (100 - loss);
}
int main() {
   int SP, profit, loss;
   SP = 1020;
   profit = 20;
   cout << "Cost Price When Profit: " << priceWhenProfit(SP, profit) << endl;
   SP = 900;
   loss = 10;
   cout << "Cost Price When loss: " << priceWhenLoss(SP, loss) << endl;
}

Output

Cost Price When Profit: 850
Cost Price When loss: 1000
raja
Published on 18-Dec-2019 07:01:15
Advertisements