Challenge: Shopping Bill
Completed
Read n, then n items as name quantity price (price is a decimal). Print each item's line total as name: total with 2 decimals, then the grand total. If the grand total is more than 100, take 10% off and also print the discounted total.
Input
n (1 ≤ n ≤ 20), then n lines of a name, a whole number and a decimal.
Output
One line per item, then Total: X.XX, and if it applies, After discount: Y.YY.
Example
Input
3 bread 2 1.5 milk 1 0.99 eggs 12 0.25
Output
bread: 3.00 milk: 0.99 eggs: 3.00 Total: 6.99
Editor
C
Hints
Read the price with %lf.
Line total = quantity * price.
10% off means multiplying by 0.9.
Solution explanation
Each line total is printed as we go and added to a running total, which decides the discount at the end.
Show solution
#include <stdio.h>
typedef struct {
char name[30];
int quantity;
double price;
} Item;
int main() {
int n;
Item items[20];
scanf("%d", &n);
double total = 0;
for (int i = 0; i < n; i++) {
scanf("%s %d %lf", items[i].name, &items[i].quantity, &items[i].price);
double line = items[i].quantity * items[i].price;
printf("%s: %.2f\n", items[i].name, line);
total += line;
}
printf("Total: %.2f\n", total);
if (total > 100) {
printf("After discount: %.2f\n", total * 0.9);
}
return 0;
} Output
Answer
You can practice without logging in. Log in to save completion.
Save your progress
You're practising as a guest, so your progress won't be saved.
- Track every lab you complete
- Earn verifiable lab certificates
- See your progress in every module