Exercise: Price After Discount in C++
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.
Understanding the Problem
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 arithmetic errors.
Approach
To solve this problem, we need to follow these steps:
- Calculate the discount amount by multiplying the original 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:
discount_amount = (price * discount) / 100 final_price = price - discount_amount
This approach is straightforward and efficient, with a time complexity of O(1) since it involves a constant number of arithmetic operations.
Algorithm
Here is a step-by-step breakdown of the algorithm:
- Read the original price and discount percentage.
- Calculate the discount amount using the formula:
discount_amount = (price * discount) / 100. - Compute the final price by subtracting the discount amount from the original price:
final_price = price - discount_amount. - Print the final price.
Code Implementation
#include <iostream> // Include the iostream library for input and output
int main() {
double price; // Variable to store the original price
double discount; // Variable to store the discount percentage
// Read the original price and discount percentage from the user
std::cout << "Enter the original price: ";
std::cin >> price;
std::cout << "Enter the discount percentage: ";
std::cin >> discount;
// Calculate the discount amount
double discount_amount = (price * discount) / 100;
// Calculate the final price after applying the discount
double final_price = price - discount_amount;
// Print the final price
std::cout << "The price after discount is: $" << final_price << std::endl;
return 0; // Return 0 to indicate successful execution
}
Complexity Analysis
The time complexity of this solution is O(1) because it involves a constant number of arithmetic operations, regardless of the input values. The space complexity is also O(1) as we are using a fixed amount of extra space for variables.
Edge Cases
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.
- Negative prices or discounts should be handled appropriately, possibly with input validation.
Testing
To test the solution comprehensively, consider the following test cases:
- Original price: 150, Discount: 10% (Expected output: 135)
- Original price: 200, Discount: 0% (Expected output: 200)
- Original price: 100, Discount: 100% (Expected output: 0)
- Original price: 50, Discount: 25% (Expected output: 37.5)
Use a variety of test cases, from simple to complex, to ensure the solution works correctly in all scenarios.
Thinking and Problem-Solving Tips
When approaching such problems, consider the following tips:
- Break down the problem into smaller, manageable steps.
- Ensure you understand the mathematical operations involved.
- Think about edge cases and how to handle them.
- Practice similar problems to improve your problem-solving skills.
Conclusion
In this blog post, we discussed how to calculate the price after applying a discount using C++. 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. Practice and explore further to enhance your problem-solving skills.
Additional Resources
For further reading and practice, consider the following resources: