C Online Compiler
Example: Basic Fixed-Item Bill in C
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// Basic Fixed-Item Bill #include <stdio.h> int main() { // Step 1: Declare variables for item details and calculations char customerName[50]; int qtyLaptop, qtyMouse, qtyKeyboard; float priceLaptop = 1200.00; float priceMouse = 25.00; float priceKeyboard = 75.00; float totalLaptop, totalMouse, totalKeyboard, subtotal; // Step 2: Get customer name printf("Enter Customer Name: "); // Use fgets to safely read a line of text including spaces // stdin is the standard input stream // 49 is the max characters to read (50 - 1 for null terminator) fgets(customerName, sizeof(customerName), stdin); // Remove the trailing newline character if present for (int i = 0; customerName[i] != '\0'; i++) { if (customerName[i] == '\n') { customerName[i] = '\0'; break; } } // Step 3: Get quantities for each item printf("Enter Quantity for Laptop (%.2f each): ", priceLaptop); scanf("%d", &qtyLaptop); printf("Enter Quantity for Mouse (%.2f each): ", priceMouse); scanf("%d", &qtyMouse); printf("Enter Quantity for Keyboard (%.2f each): ", priceKeyboard); scanf("%d", &qtyKeyboard); // Step 4: Calculate total for each item totalLaptop = qtyLaptop * priceLaptop; totalMouse = qtyMouse * priceMouse; totalKeyboard = qtyKeyboard * priceKeyboard; // Step 5: Calculate subtotal subtotal = totalLaptop + totalMouse + totalKeyboard; // Step 6: Print the bill statement printf("\n--- Bill Statement ---\n"); printf("Customer Name: %s\n", customerName); printf("----------------------------------------\n"); printf("%-15s %-5s %-10s %-10s\n", "Item", "Qty", "Price", "Total"); printf("----------------------------------------\n"); printf("%-15s %-5d %-10.2f %-10.2f\n", "Laptop", qtyLaptop, priceLaptop, totalLaptop); printf("%-15s %-5d %-10.2f %-10.2f\n", "Mouse", qtyMouse, priceMouse, totalMouse); printf("%-15s %-5d %-10.2f %-10.2f\n", "Keyboard", qtyKeyboard, priceKeyboard, totalKeyboard); printf("----------------------------------------\n"); printf("%-32s %-10.2f\n", "Subtotal:", subtotal); printf("----------------------------------------\n"); return 0; }
Output
Clear
ADVERTISEMENTS