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.
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
BufferedReaderreads each line typed by the user, andDouble.parseDouble()converts it to a double, because prices can have paise.- An
if-else if-elseladder decides between loss, profit and neither. Math.round(lossPercent * 100) / 100.0rounds 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
- Accept the cost price and the loss percentage and calculate the selling price.
- 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.
- 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.
Related Posts
Continue learning with these related articles
Sum of First n Natural Numbers in Java (Loop and Formula)
Find the sum of the first n natural numbers in Java with a for loop, a while loop and the formula n(n + 1)/2, with a dry run and the sums of even numbers, odd numbers, squares and cubes.
Prime Number Program in Java: Check a Number and Print Primes in a Range
Check whether a number is prime in Java by counting its factors, a faster square-root method with isPrime(), and a program that prints all prime numbers in a range.

Encapsulation, Access Specifiers and Scope of Variables in Java for ICSE Class 10
ICSE Class 10 Computer Applications notes on encapsulation: private, default, protected and public access, data hiding with getter and setter methods, instance vs static (class) variables, local variables and scope, with a solved BankAccount program.