Solved ProgramsICSEClass 9Computer Applications

Loss Percentage Program in Java

Java program to input the cost price and selling price and calculate the loss and loss percentage, or the profit percentage, with formulas and output.

Updated
Solved Programs

Write a program to input the cost price and selling price of an article and calculate the loss and the loss percentage. Extend it to print the profit percentage when the selling price is more than the cost price.

Formulas

  • Loss = Cost price − Selling price (when SP < CP)
  • Loss % = Loss ÷ Cost price × 100
  • Profit = Selling price − Cost price (when SP > CP)
  • Profit % = Profit ÷ Cost price × 100

Both percentages are always worked out on the cost price. For CP = 2000 and SP = 1789, the loss is 211 and the loss percentage is 211 ÷ 2000 × 100 = 10.55%.

Program

import java.io.*;

class LossPercent
{
    public static void main(String[] args) throws IOException
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter the cost price");
        double cp = Double.parseDouble(br.readLine().trim());
        System.out.println("Enter the selling price");
        double sp = Double.parseDouble(br.readLine().trim());

        if (sp < cp)
        {
            double loss = cp - sp;
            double lossPercent = loss / cp * 100;
            System.out.println("Loss = " + loss);
            System.out.println("Loss percent = " + Math.round(lossPercent * 100) / 100.0 + "%");
        }
        else if (sp > cp)
        {
            double profit = sp - cp;
            double profitPercent = profit / cp * 100;
            System.out.println("Profit = " + profit);
            System.out.println("Profit percent = " + Math.round(profitPercent * 100) / 100.0 + "%");
        }
        else
        {
            System.out.println("No profit, no loss");
        }
    }
}

Output:

Enter the cost price
2000
Enter the selling price
1789
Loss = 211.0
Loss percent = 10.55%
Enter the cost price
500
Enter the selling price
600
Profit = 100.0
Profit percent = 20.0%

How the program works

  1. BufferedReader reads each line typed by the user, and Double.parseDouble() converts it to a double, because prices can have paise.
  2. An if-else if-else ladder decides between loss, profit and neither.
  3. Math.round(lossPercent * 100) / 100.0 rounds the answer to two decimal places, so 10.549999… is printed as 10.55.

Variable description

  • cp — double — cost price of the article
  • sp — double — selling price of the article
  • loss — double — loss amount, cp − sp
  • lossPercent — double — loss as a percentage of the cost price
  • profit, profitPercent — double — profit amount and percentage when sp > cp

Practice questions

  1. Accept the cost price and the loss percentage and calculate the selling price.
  2. A shopkeeper gives a 10% discount on the marked price and still makes a 20% profit. Accept the cost price and find the marked price.
  3. Rewrite the program using the Scanner class.

More beginner programs: Solved programs for Class 9, Input using the Scanner class and the ICSE Class 9 hub.