Skip to main content

RECORD.txt item cost with VAT

Short answer

Read n items (code, desc, rate, qty). For each, cost = rate × qty. Compute subtotal, VAT = 15% of subtotal, service charge = 5% of subtotal, total = subtotal + VAT + SC. Write formatted table to RECORD.txt with thousands separators, then display the file.

Write a program in C programming language according to the output displayed below: (to open a file named RECORD.txt for the n number of data where Cost, Service Charge 5%, VAT 15%, and Total Cost must be calculated by program itself). Output is:

Item CodeDescriptionRateQuantityCost
001CTComputer22,000.005110,000.00
007MCell Phone8,000.001080,000.00
VAT 15%
Service Charge 5%
Total Cost

Thank You!!! Visit Again

Assumptions.

  • For each item: Cost = Rate × Quantity.
  • Service charge is 5% and VAT is 15% of the total of the item costs (sub-total), as the question says "calculated by the program itself".
  • Total Cost = sub-total + VAT + service charge.
  • A helper function money() prints the amounts with thousands separators as in the required output.

The program asks for n, reads the code, description, rate and quantity of each item, writes the formatted table to RECORD.txt with fprintf(), and finally reopens the file and displays it.

#include <stdio.h>

/* format a value with thousands separators, e.g. 110000.00 -> 110,000.00 */
void money(double v, char *out)
{
    long whole = (long)v;
    int paisa = (int)((v - whole) * 100 + 0.5);
    char tmp[30];
    int len, k = 0;

    sprintf(tmp, "%ld", whole);
    for (len = 0; tmp[len]; len++)
        ;
    for (int i = 0; i < len; i++) {
        if (i > 0 && (len - i) % 3 == 0)
            out[k++] = ',';
        out[k++] = tmp[i];
    }
    sprintf(out + k, ".%02d", paisa);
}

int main(void)
{
    FILE *fp;
    char code[10], desc[30], a[30], b[30], c[30];
    int n, qty;
    float rate, cost, total = 0;

    fp = fopen("RECORD.txt", "w");
    if (fp == NULL) {
        printf("Cannot open RECORD.txt\n");
        return 1;
    }
    printf("Number of items: ");
    scanf("%d", &n);
    fprintf(fp, "%-18s %-12s %10s %5s %12s\n", "Item Code", "Description", "Rate", "Qty", "Cost");
    for (int i = 0; i < n; i++) {
        printf("Code, description, rate, quantity: ");
        scanf("%9s %29s %f %d", code, desc, &rate, &qty);
        cost = rate * qty;
        total += cost;
        money(rate, a);
        money(cost, b);
        fprintf(fp, "%-18s %-12s %10s %5d %12s\n", code, desc, a, qty, b);
    }
    printf("\n");
    float vat = total * 0.15f, sc = total * 0.05f, grand = total + vat + sc;
    money(vat, a);   fprintf(fp, "%-18s %-12s %10s %5s %12s\n", "VAT 15%", "", "", "", a);
    money(sc, b);    fprintf(fp, "%-18s %-12s %10s %5s %12s\n", "Service Charge 5%", "", "", "", b);
    money(grand, c); fprintf(fp, "%-18s %-12s %10s %5s %12s\n", "Total Cost", "", "", "", c);
    fprintf(fp, "\nThank You!!! Visit Again\n");
    fclose(fp);

    fp = fopen("RECORD.txt", "r");           /* show the file on screen */
    int ch;
    while ((ch = fgetc(fp)) != EOF)
        putchar(ch);
    fclose(fp);
    return 0;
}

Sample run (the two items of the question; description of the phone is written without a space)

Input (entered one after another): 2 001CT Computer 22000 5 007M CellPhone 8000 10

Item Code          Description        Rate   Qty         Cost
001CT              Computer      22,000.00     5   110,000.00
007M               CellPhone      8,000.00    10    80,000.00
VAT 15%                                             28,500.00
Service Charge 5%                                    9,500.00
Total Cost                                         228,000.00

Thank You!!! Visit Again

Calculation: sub-total = 110,000 + 80,000 = 190,000; VAT 15% = 28,500; service charge 5% = 9,500; total = 228,000.

✓ Answer checked by Claude Opus 5.5 (high effort)

All Computer Programming chapter 10 questions →