Print a bill with left-aligned item names, right-aligned quantities and prices with two decimals, and a total line.
#include <stdio.h>
int main(void) {
const char *items[] = {"Notebook", "Pen", "Geometry box"};
int qty[] = {3, 10, 1};
double price[] = {45.0, 12.5, 149.99};
double total = 0;
printf("%-14s %4s %9s\n", "Item", "Qty", "Amount");
for (int i = 0; i < 3; i++) {
double amount = qty[i] * price[i];
total += amount;
printf("%-14s %4d %9.2f\n", items[i], qty[i], amount);
}
printf("%-14s %4s %9.2f\n", "Total", "", total);
return 0;
}
Item Qty Amount Notebook 3 135.00 Pen 10 125.00 Geometry box 1 149.99 Total 409.99
Using the same widths in the header and in every row keeps the columns aligned: %-14s left-aligns text, %4d and %9.2f right-align numbers.