Profit or Loss: flowchart, algorithm and program
This program reads the cost price (cp) and selling price (sp) and tells you whether there was a profit, a loss, or neither. It compares the two prices and prints the difference as the amount.
▶ Watch the dry run step by step →
Algorithm
- Step 1: Start.
- Step 2: Read cp.
- Step 3: Read sp.
- Step 4: If sp > cp, go to step 5; otherwise go to step 6.
- Step 5: Print Profit of sp - cp. Go to step 9.
- Step 6: If cp > sp, go to step 7; otherwise go to step 8.
- Step 7: Print Loss of cp - sp. Go to step 9.
- Step 8: Print No profit no loss.
- Step 9: Stop.
Pseudocode
BEGIN
READ cp
READ sp
IF sp > cp THEN
PRINT Profit of sp - cp
ELSE
IF cp > sp THEN
PRINT Loss of cp - sp
ELSE
PRINT No profit no loss
END IF
END IF
END
Program in C, Python and Java
Profit or Loss program in C
#include <stdio.h>
int main() {
float cp, sp;
printf("Enter cost price: ");
scanf("%f", &cp);
printf("Enter selling price: ");
scanf("%f", &sp);
if (sp > cp)
printf("Profit of %.2f\n", sp - cp);
else if (cp > sp)
printf("Loss of %.2f\n", cp - sp);
else
printf("No profit no loss\n");
return 0;
}Profit or Loss program in Python
cp = float(input("Enter cost price: "))
sp = float(input("Enter selling price: "))
if sp > cp:
print("Profit of %.2f" % (sp - cp))
elif cp > sp:
print("Loss of %.2f" % (cp - sp))
else:
print("No profit no loss")Profit or Loss program in Java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter cost price: ");
float cp = sc.nextFloat();
System.out.print("Enter selling price: ");
float sp = sc.nextFloat();
if (sp > cp)
System.out.printf("Profit of %.2f%n", sp - cp);
else if (cp > sp)
System.out.printf("Loss of %.2f%n", cp - sp);
else
System.out.println("No profit no loss");
}
}
Sample input: cp = 500, sp = 650
Output: Enter cost price: Enter selling price: Profit of 150.00
How it works
- Read cp and sp as float values so prices with decimals like 499.50 also work.
- If sp > cp, you sold for more than you paid, so it prints a profit of sp - cp.
- Otherwise, if cp > sp, you sold for less than you paid, so it prints a loss of cp - sp (cp minus sp, so the amount is positive).
- If neither is true, the two prices are equal, so it prints 'No profit no loss' and no amount.
- For cp = 500 and sp = 650, sp > cp is true, so the output is Profit of 150.00 (650 - 500, shown with 2 decimals by %.2f).
Dry run (trace table)
| Step | Line | Box | cp | sp | Output |
|---|---|---|---|---|---|
| 1 | 3 | Start | |||
| 2 | 6 | Read cp | 500 | Enter cost price: | |
| 3 | 8 | Read sp | 500 | 650 | Enter selling price: |
| 4 | 9 | sp > cp? | 500 | 650 | |
| 5 | 10 | Print Profit of sp - cp | 500 | 650 | Profit of 150.00 |
| 6 | 15 | Stop | 500 | 650 |
Time and space complexity
Time: O(1) · Space: O(1). The program does a fixed number of reads, comparisons and prints regardless of the input values.
Common mistakes
- Printing sp - cp for a loss, which gives a negative number. Use cp - sp so the loss amount is positive.
- Using %d to read or print float variables, which gives wrong values. Use %f in scanf and %.2f in printf.
- Writing separate if statements without else, or using = instead of == when comparing, which breaks the logic.
Viva questions
Q1. What does this program do, and what are its inputs and outputs?
It reads the cost price and selling price, compares them, and prints the profit amount, the loss amount, or 'No profit no loss' if both are equal.
Q2. Why have you declared cp and sp as float and not int?
Prices can have decimal parts like 99.50, and an int would drop the fractional part. float stores them, and %f in scanf reads them.
Q3. Why is there an & before cp and sp in scanf, but not in printf?
scanf needs the address of the variable so it can store the input there. printf only needs the value to display, so no & is used.
Q4. What does the condition sp > cp on line 9 check, and what is printed when it is true?
It checks whether the item was sold for more than it cost. If true, it prints a profit of sp - cp, which is the positive difference.
Q5. Why do you print sp - cp for profit but cp - sp for loss?
In each branch the larger value is subtracted from the smaller one, so the printed amount is always positive. Loss is shown as a positive number with the word 'Loss'.
Q6. What happens if cp and sp are equal, for example both 100? What if the user enters 0 or a negative price?
Both if conditions are false, so the else branch prints 'No profit no loss'. The program does not validate input, so 0 or negative prices are accepted and compared as plain numbers, which makes no real-world sense.
Q7. Is comparing floats with > and equality by the else branch always safe? What is the concern?
Floats store decimals approximately, so two values that look equal, like results of calculations, may differ slightly. Here the values are typed directly and compared as they are, so it works, but for computed values you would compare with a small tolerance.
Q8. What is the time complexity of this program?
It is O(1), constant time, because it does a fixed number of reads, one comparison chain and one print, with no loops.
Q9. What would you change to also print the profit or loss percentage?
In the profit branch, print (sp - cp) / cp * 100, and in the loss branch print (cp - sp) / cp * 100. You would also have to make sure cp is not 0 to avoid division by zero.
Frequently asked questions
What is the formula for profit and loss?
Profit = selling price - cost price when sp is greater than cp. Loss = cost price - selling price when cp is greater than sp.
Why does the program use float instead of int?
Prices often have decimals, such as 99.99. A float lets the program handle them, and %.2f prints the amount with two decimal places.
Why is there no condition for the last else?
If sp is not greater than cp and cp is not greater than sp, the only possibility left is that they are equal. So a plain else covers the no profit no loss case.