“Apply Conditional Discount on Products in an Array Using C”
C
Medium
3 views
Problem Description
The array contains product precedences, you need to apply a discount: 10% on the first 5 items, 5% on the rest - how do you handle the conditional discount logic?
Official Solution
#include <stdio.h>
int main() {
float prices[] = {100, 200, 150, 120, 180, 250, 300};
int size = sizeof(prices) / sizeof(prices[0]);
float discounted[size];
for (int i = 0; i < size; i++) {
if (i < 5) {
// First 5 items: 10% discount
discounted[i] = prices[i] * 0.90;
} else {
// Remaining items: 5% discount
discounted[i] = prices[i] * 0.95;
}
}
printf("Original and discounted prices:n");
for (int i = 0; i < size; i++) {
printf("Item %d: Original = %.2f, Discounted = %.2fn", i+1, prices[i], discounted[i]);
}
return 0;
}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!