Profit or Loss Calculator - Problem
Given the cost price and selling price of an item, determine whether there is a profit, loss, or break-even situation, and calculate the percentage.
Return a string in the format: "Profit: X.XX%", "Loss: X.XX%", or "Break-even"
Formula:
- If selling price > cost price: Profit% = ((selling price - cost price) / cost price) × 100
- If selling price < cost price: Loss% = ((cost price - selling price) / cost price) × 100
- If selling price = cost price: Break-even
Input & Output
Example 1 — Profit Case
$
Input:
costPrice = 100.00, sellingPrice = 120.00
›
Output:
"Profit: 20.00%"
💡 Note:
Selling price (120) > cost price (100), so profit = (120-100)/100 × 100 = 20.00%
Example 2 — Loss Case
$
Input:
costPrice = 150.00, sellingPrice = 120.00
›
Output:
"Loss: 20.00%"
💡 Note:
Selling price (120) < cost price (150), so loss = (150-120)/150 × 100 = 20.00%
Example 3 — Break-even Case
$
Input:
costPrice = 100.00, sellingPrice = 100.00
›
Output:
"Break-even"
💡 Note:
Selling price equals cost price, so no profit or loss
Constraints
- 0 < costPrice ≤ 106
- 0 < sellingPrice ≤ 106
- Prices are given as floating-point numbers
- Percentage should be formatted to 2 decimal places
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code