#include <stdio.h> struct Item { char name[50]; float price; int quantity; }; float calculateTotal(struct Item items[], int itemCount) { float total = 0.0; for (int i = 0; i < itemCount; i++) { total += items[i].price * items[i].quantity; } return total; } float calculateDiscountedTotal(float total, float discount) { return total - (total * discount / 100); } float calculateRemainingCash(float total, float cashInput) { return cashInput - total; } void displayBill(struct Item items[], int itemCount, float total, float discount, float cashInput, float remainingCash) { printf("\n********** BILL **********\n"); printf("Item\t\tPrice\tQuantity\n"); printf("----------------------------\n"); for (int i = 0; i < itemCount; i++) { printf("%s\t\t%.2f\t%d\n", items[i].name, items[i].price, items[i].quantity); } printf("----------------------------\n"); printf("Total: %.2f\n", total); printf("Discount: %.2f%%\n", discount); printf("Discounted Total: %.2f\n", calculateDiscountedTotal(total, discount)); printf("Cash Input: %.2f\n", cashInput); printf("Remaining Cash: %.2f\n", remainingCash); printf("****************************\n"); } int main() { int itemCount; printf("Enter the number of items: "); scanf("%d", &itemCount); struct Item items[itemCount]; printf("Enter the details of each item:\n"); for (int i = 0; i < itemCount; i++) { printf("Item %d:\n", i + 1); printf("Name: "); scanf("%s", items[i].name); printf("Price: "); scanf("%f", &items[i].price); printf("Quantity: "); scanf("%d", &items[i].quantity); } float total = calculateTotal(items, itemCount); float discount; printf("Enter discount percentage: "); scanf("%f", &discount); float cashInput; printf("Enter cash input: "); scanf("%f", &cashInput); float remainingCash = calculateRemainingCash(calculateDiscountedTotal(total, discount), cashInput); displayBill(items, itemCount, total, discount, cashInput, remainingCash); return 0; }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter