Skip to main content

← Back to Overview

Profit or Loss Calculator

Concept Explanation

What is it?

This program calculates whether a transaction results in a profit, a loss, or a break-even scenario. It takes two primary inputs: the cost price (the price at which an item was bought) and the selling price (the price at which an item was sold).

  • Profit: Occurs when the selling price is greater than the cost price.
  • Loss: Occurs when the selling price is less than the cost price.
  • No Profit, No Loss (Break-even): Occurs when the selling price is equal to the cost price.

Why is it important?

Understanding profit and loss is a fundamental concept in business and economics. In programming, it serves as an excellent practical example for applying conditional logic (if-else statements) to make decisions based on numerical comparisons.

Where is it used?

  • Business Applications: Inventory management, sales tracking, financial reporting.
  • E-commerce Platforms: Calculating margins, discounts, and promotional offers.
  • Personal Finance Tools: Budgeting, tracking investments.
  • Algorithmic Trading: Determining entry/exit points for trades.

Real-world example

Imagine a small shop owner buying a toy for $10 (cost price). If they sell it for $15, they make a profit of $5. If they sell it for $8, they incur a loss of $2. If they sell it for $10, it's a break-even. This program automates this simple calculation.


Algorithm

  1. Start.
  2. Get the cost_price from the user.
  3. Get the selling_price from the user.
  4. Calculate the difference = selling_price - cost_price.
  5. If difference is greater than 0: a. Display "Profit" and the value of difference.
  6. Else if difference is less than 0: a. Display "Loss" and the absolute value of difference.
  7. Else (difference is 0): a. Display "No profit, no loss."
  8. End.

Edge Cases:

  • Non-numeric input for prices (handled by language-specific error mechanisms or explicit validation).
  • Zero prices (e.g., free items) should be handled logically by the comparisons.

Implementations

import java.util.Scanner;

public class ProfitOrLoss {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.print("Enter the cost price: ");
double costPrice = scanner.nextDouble();

System.out.print("Enter the selling price: ");
double sellingPrice = scanner.nextDouble();

System.out.println("Cost Price: $" + costPrice);
System.out.println("Selling Price: $" + sellingPrice);

if (sellingPrice > costPrice) {
double profit = sellingPrice - costPrice;
System.out.println("The seller is in PROFIT of $" + String.format("%.2f", profit));
} else if (sellingPrice < costPrice) {
double loss = costPrice - sellingPrice;
System.out.println("The seller is in LOSS of $" + String.format("%.2f", loss));
} else {
System.out.println("No profit, no loss.");
}

scanner.close();
}
}

Explanation

  • Java: Uses if-else if-else structure for comparison. Scanner handles double input. String.format("%.2f", ...) ensures output is formatted to two decimal places for currency.
  • Python: Similar if-elif-else logic. float() converts string inputs. f-strings are used for formatted currency output.
  • C: Employs if-else if-else statements. scanf("%lf", ...) reads double inputs, and printf("$%.2lf", ...) formats output. Uses -difference to display a positive loss amount.
  • Oracle: Implemented in PL/SQL. Uses IF-ELSIF-ELSE blocks. ABS() function is used to display the absolute value of loss. Substitution variables for interactive input.

Complexity Analysis

  • Time Complexity: O(1) - A fixed number of arithmetic operations and comparisons.
  • Space Complexity: O(1) - A fixed number of variables are used.

Flowchart

Sample Dry Run

StepCost PriceSelling PriceDifferenceDescription
Input100120-User enters 100, 120
Process10012020Difference = 120 - 100 = 20
Decision--2020 > 0? (True)
Output---Display "PROFIT of $20.00"
End---Program terminates
Input10080-User enters 100, 80
Process10080-20Difference = 80 - 100 = -20
Decision---20-20 > 0? (False)
Decision---20-20 < 0? (True)
Output---Display "LOSS of $20.00"
End---Program terminates
Input5050-User enters 50, 50
Process50500Difference = 50 - 50 = 0
Decision--00 > 0? (False)
Decision--00 < 0? (False)
Output---Display "No profit, no loss."
End---Program terminates

Practice Problems

Easy

  • Modify the program to also calculate the profit/loss percentage.
  • Extend the program to handle multiple items.

Medium

  • Write a program that calculates the total profit/loss for a series of transactions.
  • Implement a simple inventory system where profit/loss is automatically calculated upon sales.

Hard

  • Develop a financial analysis tool that visualizes profit/loss trends over time.

"The most important property of a program is whether it accomplishes the intention of its user." - C.A.R. Hoare

📍 Visit Us

🏫 VD Computer Tuition Surat

VD Computer Tuition
📍 Address
2/66 Faram Street, Rustompura
Surat395002, Gujarat, India
📞 Phone / WhatsApp
+91 84604 41384
🌐 Website

Computer Classes & Tuition — Areas We Serve in Surat

AdajanAlthanAmroliAthwaAthwalinesBhagalBhatarBhestanCanal RoadChowkCitylightDumasGaurav PathGhod Dod RoadHaziraJahangirpuraKamrejKapodraKatargamLimbayatMagdallaMajura GateMota VarachhaNanpuraNew CitylightOlpadPalPandesaraParle PointPiplodPunaRanderRing RoadRustampuraSachinSalabatpuraSarthanaSosyo CircleUdhnaVarachhaVed RoadVesuVIP Road
📞 Call Sir💬 WhatsApp Sir