---
title: "RECORD.txt item cost with VAT"
subject: "Computer Programming"
chapter: 10
key: "semester_1/computer_programming:file-record-txt-invoice"
source: https://www.bctengineer.com/q/semester_1/computer_programming/file-record-txt-invoice
site: BCT Engineering
---

# RECORD.txt item cost with VAT

Computer Programming · Chapter 10: Data Files

- Asked in: 2067 Asar, 10 marks
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_1/computer_programming/file-record-txt-invoice
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-10

## Question

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 Code | Description | Rate | Quantity | Cost |
|---|---|---|---|---|
| 001CT | Computer | 22,000.00 | 5 | 110,000.00 |
| 007M | Cell Phone | 8,000.00 | 10 | 80,000.00 |
| VAT 15% | | | | |
| Service Charge 5% | | | | |
| Total Cost | | | | |

Thank You!!! Visit Again

## 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.

## Answer

**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.

```c
#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`

```text
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.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. On which amount are VAT 15% and service charge 5% calculated in the program?
   A. On each item's rate separately
   B. On the sub-total, the sum of all item costs
   C. On the quantity of items
   D. On the total cost after adding them
   Answer: B. Both percentages are taken from the sub-total of the item costs (110,000 + 80,000 = 190,000). (Key point: Basis of VAT and service charge)

2. For the two items of the question (sub-total 190,000), what is the Total Cost?
   A. 209,000.00
   B. 218,500.00
   C. 228,000.00
   D. 190,000.00
   Answer: C. VAT = 0.15 x 190,000 = 28,500 and service charge = 0.05 x 190,000 = 9,500, so total = 190,000 + 28,500 + 9,500 = 228,000. (Key point: Total cost)

3. How does the program compute the Cost column for each item?
   A. Rate + Quantity
   B. Rate x Quantity
   C. Rate / Quantity
   D. Quantity x 15%
   Answer: B. Cost = Rate x Quantity, e.g. 22,000 x 5 = 110,000. (Key point: Cost of an item)

4. What is the job of the helper function money()?
   A. It converts rupees to dollars
   B. It converts a number to text with commas and two decimals, such as 110,000.00
   C. It adds VAT to a value
   D. It reads the amount from the keyboard
   Answer: B. printf cannot insert thousands separators, so money() builds the string with a comma after every three digits and then adds .xx. (Key point: Thousands separators)

## Related in Computer Programming

- [96 students record; pass marks](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afile-96-students-ioe-txt) — Computer Programming, chapter 10
- [Copy contents of one file to another](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afile-copy-contents) — Computer Programming, chapter 10, asked 2×
- [INVENTORY file of items](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afile-inventory) — Computer Programming, chapter 10
- [Read records until end; show nth record](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afile-worker-nth-record) — Computer Programming, chapter 10, asked 2×
- [Define programming language](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Adefine-programming-language) — Computer Programming, chapter 1, asked 3×
- [Output of arithmetic expression program](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aoutput-expression-xyz) — Computer Programming, chapter 3

## Related in other subjects

- [Student file displayed in descending marks order](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Astudent-file-descending) — Object Oriented Programming, chapter 8
- [File stream; student binary file program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Afile-stream-student-binary) — Object Oriented Programming, chapter 8
- [Total cost of apartment building](https://www.bctengineer.com/question.md?key=semester_7%2Festimating_and_costing%3Atotal-cost-apartment-500) — Estimating and Costing, chapter 3
- [EOQ, total cost and reorder point numerical](https://www.bctengineer.com/question.md?key=semester_6%2Findustrial_engineering_and_management%3Aeoq-numerical-reorder-point) — Industrial Engineering and Management, chapter 4
