C Program to Store and Display Records of N Products Using Structure (स्ट्रक्चर का उपयोग करके N प्रोडक्ट्स के रिकार्ड्स स्टोर और डिस्प्ले करना)
In C language, an array of structures is used to store and display records of multiple products. Each product record contains product ID, product name, manufacturing date, supplier name and price.
C Language में Multiple Products के Records को Store और Display करने के लिए Array of Structures का उपयोग किया जाता है। प्रत्येक Product के Record में Product ID, Product Name, Manufacturing Date, Supplier Name और Price शामिल होते हैं।
C Program
#include <stdio.h>#include <string.h>#define N 10struct product{int pid;char pname[20];char mfd[11];char sname[20];float price;};int main(){int i;struct product p[N];// Input product recordsfor (i = 0; i < N; i++){printf("\nEnter details of Product %d:\n", i + 1);printf("Enter product ID: ");scanf("%d", &p[i].pid);getchar();printf("Enter product name: ");fgets(p[i].pname, sizeof(p[i].pname), stdin);p[i].pname[strcspn(p[i].pname, "\n")] = '\0';printf("Enter manufacturing date (DD/MM/YYYY): ");fgets(p[i].mfd, sizeof(p[i].mfd), stdin);p[i].mfd[strcspn(p[i].mfd, "\n")] = '\0';printf("Enter supplier name: ");fgets(p[i].sname, sizeof(p[i].sname), stdin);p[i].sname[strcspn(p[i].sname, "\n")] = '\0';printf("Enter price: ");scanf("%f", &p[i].price);}// Display product recordsprintf("\n========== PRODUCT RECORDS ==========\n");for (i = 0; i < N; i++){printf("\nDetails of Product %d:\n", i + 1);printf("Product ID : %d\n", p[i].pid);printf("Product Name : %s\n", p[i].pname);printf("Manufacturing Date : %s\n", p[i].mfd);printf("Supplier Name : %s\n", p[i].sname);printf("Price : Rs. %.2f\n", p[i].price);}return 0;}
Sample Output
Enter details of Product 1:Enter product ID: 101Enter product name: LaptopEnter manufacturing date (DD/MM/YYYY): 15/08/2026Enter supplier name: ABC TradersEnter price: 45000========== PRODUCT RECORDS ==========Details of Product 1:Product ID : 101Product Name : LaptopManufacturing Date : 15/08/2026Supplier Name : ABC TradersPrice : Rs. 45000.00... Records of remaining products ...
Note: The program accepts and displays records of all 10 products.
Explanation
Structure Declaration:
struct productdefines five members to store product details.struct productमें Product की जानकारी Store करने के लिए पाँच Members Declare किए गए हैं।Array of Structures:
struct product p[N];creates an array to store 10 product records.यह Statement 10 Products के Records Store करने के लिए Array बनाता है।
Input: The first
forloop accepts product details from the user.पहला
forLoop यूजर से सभी Products की जानकारी Input करता है।Display: The second
forloop displays all stored product records.दूसरा
forLoop Store किए गए सभी Product Records को Display करता है।Member Access: The dot (
.) operator accesses individual structure members.Dot (
.) Operator की सहायता से Structure के अलग-अलग Members को Access किया जाता है।
Important Points
#define N 10specifies the number of products.An array of structures stores multiple product records.
Manufacturing date is stored as a character array.
fgets()accepts product names and supplier names containing spaces.The price is stored using the
floatdata type.
महत्वपूर्ण बिंदु:
#define N 10Products की संख्या निर्धारित करता है।Array of Structures का उपयोग Multiple Product Records Store करने के लिए किया जाता है।
Manufacturing Date को Character Array में Store किया जाता है।
fgets()की सहायता से Space सहित Product और Supplier के नाम Input किए जा सकते हैं।Price को
floatData Type में Store किया जाता है।
Comments
Post a Comment