Java ProgramsBasics & I/OCalculate Discount Amount

Calculate Discount Amount in Java

beginner·  Basics & I/O  ·  Arithmetic

Problem

The simplest discount model applies the same fixed percentage to every price — one multiplication and division, no thresholds involved.

Given a price and a flat discount percentage, calculate the discount amount.

Input
price = 1000.0, discountPercent = 10.0
Output
Discount Amount: 100.0

Java Program

Java
public class CalculateDiscountAmount { public static void main(String[] args) { double price = 1000.0; double discountPercent = 10.0; double discountAmount = (price * discountPercent) / 100; // flat percentage of price System.out.println("Discount Amount: " + discountAmount); } }

Output

Discount Amount: 100.0

Core Logic

Multiplying the price by the discount percentage and dividing by 100 gives the discount amount directly, in a single expression.

How It Works
  1. 1price holds 1000.0 and discountPercent holds 10.0.
  2. 2(price * discountPercent) / 100 evaluates to 100.0.
  3. 3The result is stored in discountAmount and printed with a label.
For price = 1000.0 with a 10% discount, (1000.0 * 10.0) / 100 gives 100.0.
💡

Key Point: Real discount schemes often vary the percentage by how much is spent — for example, a bigger discount above a certain price threshold — which needs conditional logic rather than a single flat percentage like this one.

Key Concepts

doublearithmetic expression

Approach 2: Java 8

Java
import java.util.function.DoubleBinaryOperator; public class CalculateDiscountAmountLambda { public static void main(String[] args) { double price = 1000.0; double discountPercent = 10.0; // The discount formula is stored as a lambda, named for what it computes DoubleBinaryOperator discountOf = (p, percent) -> (p * percent) / 100; double discountAmount = discountOf.applyAsDouble(price, discountPercent); System.out.println("Discount Amount: " + discountAmount); } }

Output

Discount Amount: 100.0

Core Logic

The discount formula can be wrapped in a named lambda, turning 'compute a discount' into a small reusable function.

How It Works
  1. 1DoubleBinaryOperator discountOf = (p, percent) -> (p * percent) / 100; stores the formula as a lambda.
  2. 2discountOf.applyAsDouble(price, discountPercent) calls it, returning the same result the inline expression would.
  3. 3The result is printed exactly as before.
With price = 1000.0 and discountPercent = 10.0, discountOf.applyAsDouble(1000.0, 10.0) returns 100.0.
💡

Key Point: This is the same 'percent of an amount' shape used by the Simple Interest and Percentage pages — the same reusable idea applies just as well to a discount.

Key Concepts

DoubleBinaryOperatorfunctional interfacelambda expression

Related Programs