Discount Slabs
Calculate the discounted price based on predefined slabs and their corresponding discount percentages
Understand the Problem
Problem Statement
A shop offers discounts based on the purchase price. There are N discount slabs defined by boundary amounts and corresponding discount percentages. When a purchase price falls between two consecutive slab amounts (i.e., between the i-th and (i+1)-th slab), the discount percentage for the i-th slab is applied. If the price is below the lowest slab, no discount is applied. Calculate and output the final discounted amount with exactly two decimal places.
For example, if slabs are [1000, 2000, 3000, 4000] with discounts [12%, 16%, 18%, 24%], a price of 3400 falls between 3000 and 4000, so the 18% discount is applied.
Constraints
- 1 ≤ N ≤ 100 (number of discount slabs)
- Price is a positive floating-point number
- Slab amounts are positive integers in ascending order
- Discount percentages are positive integers between 1 and 100
- Output must be formatted to exactly 2 decimal places
Examples
4 3400
1000 2000 3000 4000
12 16 18 242788.00The price 3400 falls between slab 3000 and 4000, so the 3rd slab's discount (18%) is applied. Final price = 3400 - (3400 * 18/100) = 3400 - 612 = 2788.00
4 400
500 1000 1500 2000
10 20 30 40400.00The price 400 is below the lowest slab (500), so no discount is applied. The final price remains 400.00
Solution
#include <stdio.h>
int main() {
int n, slab_amount, discount_percent = 0;
float price;
int applicable_slab_index = -1;
// Read number of slabs and price
scanf("%d %f", &n, &price);
// Find the applicable slab
for (int i = 0; i < n; i++) {
scanf("%d", &slab_amount);
if (price >= slab_amount) {
applicable_slab_index = i;
}
}
// Read discount percentages and get the one for applicable slab
for (int i = 0; i < n; i++) {
scanf("%d", &discount_percent);
if (i == applicable_slab_index) {
break;
}
}
// Calculate and print final price
float final_price;
if (applicable_slab_index == -1) {
// No discount applies
final_price = price;
} else {
final_price = price - (price * discount_percent / 100.0);
}
printf("%.2f", final_price);
return 0;
}C Solution Explanation:
- Variable Declaration: We declare variables for number of slabs (n), slab amounts (slab_amount), discount percentage (discount_percent), price (price), and tracking the applicable slab index (applicable_slab_index)
- Input Reading: First scanf reads n and price from the first line
- Slab Processing: The first loop reads slab amounts and updates applicable_slab_index whenever the price is >= current slab amount. This finds the highest applicable slab
- Discount Processing: The second loop reads discount percentages. We break once we reach the applicable slab index, so discount_percent contains the correct discount rate
- Calculation: If no slab applies (applicable_slab_index == -1), we keep the original price. Otherwise, we apply the discount formula: price - (price * discount / 100)
- Output: printf with "%.2f" formats the result to exactly 2 decimal places