C Program to Display the Inventory of Items in a Store

«
»

This is a C Program to display the inventory of items in a store.

Problem Description

This C Program display the inventory of items in a store.

Problem Solution

The program accepts the value of item name, item code, price, quantity & manufacture date. Then display those value in a structured way.

advertisement
Program/Source Code

Here is source code of the C program to display the inventory of items in a storage. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

/*
 * C program to display the inventory of items in a store / shop
 * The inventory maintains details such as name, price, quantity
 * and manufacturing date of each item.
 */
#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");
}
Program Explanation

In this C program, integer variables are stored in the structure and the variable item[50] is used to access the integer variable stored in the structure. We are reading the number of variables using ‘n’ variable. The fflush(stdin) function will flush the input buffer of a stream.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement

Using for loop enter the name of the item using ‘item[i].name’ variable, the code of the item using the ‘item[i].code’ variable, the price of the item using the ‘item[i].price’ variable, and the manufacturing date of the item using ‘item[i].mfg.day’, ‘item[i].mfg.month’, ‘item[i].mfg.year’ variables. Then print the values in a structured way.

Runtime Test Cases
 
$ cc pgm60.c
$ a.out
Enter number of items:3
Item name:
pendrive
Item code:
123
Quantity:
6
price:
3000
Manufacturing date(dd-mm-yyyy):
30-9-2012
Item name:
computer
Item code:
124
Quantity:
10
price:
10000
Manufacturing date(dd-mm-yyyy):
30-7-2012
Item name:
optical mouse
Item code:
Quantity:
price:
Manufacturing date(dd-mm-yyyy):
             *****  INVENTORY *****
------------------------------------------------------------------
S.N.|    NAME           |   CODE   |  QUANTITY |  PRICE  | MFG.DATE
------------------------------------------------------------------
1     pendrive               123          6        3000     30/9/2012
2     computer               124          10       10000    30/7/2012
3     optical                0            0        0        0/0/0
------------------------------------------------------------------
 
$ a.out
Enter number of items:3
Item name:
pendrive
Item code:
123
Quantity:
6
price:
3000
Manufacturing date(dd-mm-yyyy):
30-9-2012
Item name:
computer
Item code:
124
Quantity:
10
price:
10000
Manufacturing date(dd-mm-yyyy):
30-7-2012
Item name:
Mouse
Item code:
125
Quantity:
10
price:
1500
Manufacturing date(dd-mm-yyyy):
30-6-2012
 
             *****  INVENTORY *****
------------------------------------------------------------------
S.N.|    NAME           |   CODE   |  QUANTITY |  PRICE    | MFG.DATE
------------------------------------------------------------------
1     pendrive               123          6        3000      30/9/2012
2     computer               124          10       10000     30/7/2012
3     Mouse                  125          10       1500      30/6/2012
------------------------------------------------------------------

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at other example programs on Simple C Programs, go to Simple C Programs. If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.