Discount Calculator in Java

Apply discount based on purchase amount using conditional logic.

BeginnerModule 2: Conditional ProgramsExample 19 of 20
discount-calculator.java
1import java.util.Scanner;
2
3public class Main {
4 public static void main(String[] args) {
5 Scanner sc = new Scanner(System.in);
6
7 System.out.print("Enter purchase amount: ");
8 double amount = sc.nextDouble();
9
10 double discount;
11 if (amount >= 5000) {
12 discount = 0.2 * amount;
13 } else if (amount >= 2000) {
14 discount = 0.1 * amount;
15 } else {
16 discount = 0.05 * amount;
17 }
18
19 double net = amount - discount;
20 System.out.println("Discount = " + discount);
21 System.out.println("Net amount to pay = " + net);
22
23 sc.close();
24 }
25}

Output

Enter purchase amount: 3000
Discount = 300.0
Net amount to pay = 2700.0

What's going on

We give higher discounts for higher purchase slabs and compute final payable amount.