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
INPUT DATACost Price$100.00Selling Price$120.00ALGORITHM STEPS1Compare: $120 > $1002Result: Profit case3Calculate: (120-100)/1004Format: 20.00% profitFINAL RESULTOUTPUT STRINGProfit: 20.00%Formatted resultKey Insight:The percentage formula is the same for profit and loss - just use absolute difference and check the sign to determine which case it is.TutorialsPoint - Profit or Loss Calculator | Mathematical Formula Approach
Asked in
Amazon 25 Google 20 Microsoft 18 Apple 15
12.5K Views
Medium Frequency
~8 min Avg. Time
450 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen