Find cost price from given selling price and profit or loss percentage | Java implementation to find Cost price ; 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
import java . util . * ; class solution { static float CPwithProfit ( int sellingPrice , int profit ) { float costPrice ; costPrice = ( sellingPrice * 100 ) / ( 100 + profit ) ; return costPrice ; } static float CPwithLoss ( int sellingPrice , int loss ) { float costPrice ; costPrice = ( sellingPrice * 100 ) / ( 100 - loss ) ; return costPrice ; } public static void main ( String args [ ] ) { int SP , profit , loss ; SP = 1020 ; profit = 20 ; System . out . println ( " Cost ▁ Price ▁ = ▁ " + CPwithProfit ( SP , profit ) ) ; SP = 900 ; loss = 10 ; System . out . println ( " Cost ▁ Price ▁ = ▁ " + CPwithLoss ( SP , loss ) ) ; SP = 42039 ; profit = 8 ; System . out . println ( " Cost ▁ Price ▁ = ▁ " + CPwithProfit ( SP , profit ) ) ; } }