We created two variables to store the original `price`

of a product in dollars and the `discount`

**percentage** applied to that price.

Use these two variables and **arithmetic operations** to compute and print the price **after the discount**.

**Example:**

For example, if `price`

was `150`

and `discount`

was `10`

, the answer would be `135`

.

Why? Because the discount is 10%. 10% of 150 dollars is 15 dollars. And 150 - 15 dollars is 135 dollars.

The core challenge of this problem is to correctly apply a percentage discount to a given price. This is a common task in various applications, such as e-commerce platforms, where discounts are frequently applied to products.

Potential pitfalls include incorrect calculations due to misunderstanding percentage operations or mishandling decimal values.

To solve this problem, we need to follow these steps:

- Calculate the discount amount by multiplying the price by the discount percentage divided by 100.
- Subtract the discount amount from the original price to get the final price after the discount.

Let's break down the steps in more detail:

- Calculate the discount amount:
`discountAmount = price * (discount / 100.0)`

- Calculate the final price:
`finalPrice = price - discountAmount`

Here is a step-by-step breakdown of the algorithm:

- Read the original price and discount percentage.
- Compute the discount amount using the formula:
`discountAmount = price * (discount / 100.0)`

- Compute the final price after discount:
`finalPrice = price - discountAmount`

- Print the final price.

```
public class PriceAfterDiscount {
public static void main(String[] args) {
// Original price of the product
double price = 150.0;
// Discount percentage
double discount = 10.0;
// Calculate the discount amount
double discountAmount = price * (discount / 100.0);
// Calculate the final price after discount
double finalPrice = price - discountAmount;
// Print the final price
System.out.println("The price after discount is: $" + finalPrice);
}
}
```

The time complexity of this solution is **O(1)** because the operations involved (multiplication and subtraction) take constant time.

The space complexity is also **O(1)** as we are using a fixed amount of extra space for variables.

Consider the following edge cases:

- If the discount is 0%, the final price should be the same as the original price.
- If the discount is 100%, the final price should be 0.
- If the price is 0, the final price should also be 0 regardless of the discount.

These edge cases can be tested by modifying the values of `price`

and `discount`

in the code.

To test the solution comprehensively, consider the following test cases:

- price = 150, discount = 10 (Expected output: 135)
- price = 200, discount = 25 (Expected output: 150)
- price = 100, discount = 0 (Expected output: 100)
- price = 100, discount = 100 (Expected output: 0)
- price = 0, discount = 50 (Expected output: 0)

When approaching such problems, it is important to:

- Understand the mathematical operations involved.
- Break down the problem into smaller, manageable steps.
- Consider edge cases and test your solution against them.
- Practice similar problems to improve your problem-solving skills.

In this blog post, we discussed how to calculate the price of a product after applying a discount. We covered the problem definition, approach, algorithm, code implementation, complexity analysis, edge cases, and testing. Understanding and solving such problems is crucial for various applications, especially in e-commerce.

We encourage you to practice and explore further to enhance your problem-solving skills.

For further reading and practice, consider the following resources: