Find cost price from given selling price and profit or loss percentage | Function to calculate cost price with profit ; required formula to calculate CP with profit ; Function to calculate cost price with loss ; required formula to calculate CP with loss ; Driver code
< ? php function CPwithProfit ( $ sellingPrice , $ profit ) { $ costPrice = ( $ sellingPrice * 100.0 ) / ( 100 + $ profit ) ; return $ costPrice ; } function CPwithLoss ( $ sellingPrice , $ loss ) { $ costPrice = ( $ sellingPrice * 100.0 ) / ( 100 - $ loss ) ; return $ costPrice ; } $ SP = 1020 ; $ profit = 20 ; echo ( " Cost ▁ Price ▁ = ▁ " ) ; echo ( CPwithProfit ( $ SP , $ profit ) ) ; echo ( " STRNEWLINE " ) ; $ SP = 900 ; $ loss = 10 ; echo ( " Cost ▁ Price ▁ = ▁ " ) ; echo ( CPwithLoss ( $ SP , $ loss ) ) ; echo ( " STRNEWLINE " ) ; $ SP = 42039 ; $ profit = 8 ; echo ( " Cost ▁ Price ▁ = ▁ " ) ; echo ( CPwithProfit ( $ SP , $ profit ) ) ; echo ( " STRNEWLINE " ) ; ? >