C Program to display the product inventory of items in a store

For a better understanding, we always recommend you to learn the basic topics of C programming listed below:

In this C program, we need to make a shop program to include the products or items name, price, quantity, etc, and to display it in a table format like billing software. For implementing this program we are using structure datatype. Here we have to make a record of different elements of the different datatype.

What is the structure datatype in C?

The structure is a user-defined data type in C. It is a set of variables with different data types which is represented by a single name. The keyword struct is used to define a structure. Using a structure data type we can store a record of different data type elements in a single memory location. For example, we can check the date using struct:

struct date
    {
       int day;
       int month;
       int year;
    };

The logic of this program is, first declare the Structure variables date, and details. Then declare an Array item of datatype details. Then read the number of Products the user wants to add into the variable n.By using a for loop with the condition i

ALGORITHM

STEP 1:Include the header files to use the built-in functions in the C program.

STEP 2: Declare the structure date with members day, month, year.

STEP 3:Declare another structure details with the member's name, price, code, qty, and mfg.

STEP 4: Initialize the structure details variable item.

STEP 5:Declare the integer variable n, i.

STEP 6: Read the number of items into n.

STEP 7:By using a for loop with the condition i

STEP 8: Display  INVENTORY.

STEP 9:Display S.N.| NAME  |   CODE   |  QUANTITY |  PRICE  |MFG.DATE using printf.

STEP 10: By using another for loop with the condition i

STEP 11:Display item[i].name,item[i].code,item[i].qty,item[i].price,item[i].mfg.day,item[i].mfg.month,item[i].mfg.year.

 

C Source Code

                                          #include <stdio.h>
 
void main()
{
    struct date
    {
        int day;
        int month;
        int year;
    };
    struct details
    {
        char name[20];
        int price;
        int code;
        int qty;
        struct date mfg;
    };
    struct details item[50];
    int n, i;
 
    printf("Enter number of items:");
    scanf("%d", &n);
    fflush(stdin);
    for (i = 0; i < n; i++)
    {
        fflush(stdin);
        printf("Item name: \n");
        scanf("%s", item[i].name);
        fflush(stdin);
        printf("Item code: \n");
        scanf("%d", &item;[i].code);
        fflush(stdin);
        printf("Quantity: \n");
        scanf("%d", &item;[i].qty);
        fflush(stdin);
        printf("price: \n");
        scanf("%d",  &item;[i].price);
        fflush(stdin);
        printf("Manufacturing date(dd-mm-yyyy): \n");
        scanf("%d-%d-%d", &item;[i].mfg.day,
        &item;[i].mfg.month, &item;[i].mfg.year);
    }
    printf("             *****  INVENTORY ***** \n");
    printf("------------------------------------------------------------------\n");
    printf("S.N.|    NAME           |   CODE   |  QUANTITY |  PRICE| MFG.DATE \n");
    printf("------------------------------------------------------------------\n");
    for (i = 0; i < n; i++)
        printf("%d     %-15s        %-d          %-5d     %-5d %d/%d/%d \n", i + 1, item[i].name, item[i].code, item[i].qty,
        item[i].price, item[i].mfg.day, item[i].mfg.month,
        item[i].mfg.year);
    printf("------------------------------------------------------------------\n");
}
                                      

OUTPUT

Enter number of items:5

Item name:Tea Powder
Item code:123
Quantity:23
price:40
Manufacturing date(dd-mm-yyyy):12-03-2007

Item name:Milk Powder
Item code:345
Quantity:20
price:80
Manufacturing date(dd-mm-yyyy):30-03-2007

Item name:Soap Powder
Item code:510
Quantity:10
price:30
Manufacturing date(dd-mm-yyyy):01-04-2007

Item name:Washing Soap
Item code:890
Quantity:25
price:12
Manufacturing date(dd-mm-yyyy):10-03-2007

Item name:Shampoo
Item code:777
Quantity:8
price:50
Manufacturing date(dd-mm-yyyy):17-05-2007

     *****  INVENTORY *****
------------------------------------------------------------------------
S.N.|    NAME           |   CODE   |  QUANTITY |  PRICE  |MFG.DATE
------------------------------------------------------------------------
1     Tea Powder           123         23         40        12/3/2007
2     Milk Powder          345         20         80        30/3/2007
3     Soap Powder          510         10         30        1/4/2007
4     Washing Soap         890         25         12        10/3/2007
5     Shampoo              777         8          50        17/5/2007
------------------------------------------------------------------------