C Program to Store and Display Records of N Products Using Union ( यूनियन का उपयोग करके N प्रोडक्ट के रिकॉर्ड स्टोर और प्रदर्शित करने का C प्रोग्राम)
In C language, an array of unions can be used to store data for multiple products. A union allows all its data members to share the same memory location. In this program, we store and display the prices of N products using an array of unions.
C भाषा में Array of Union का उपयोग करके कई products का डेटा स्टोर किया जा सकता है। Union के सभी data members एक ही memory location साझा करते हैं। इस प्रोग्राम में Array of Union का उपयोग करके N products की कीमतें स्टोर और प्रदर्शित की जाती हैं।
C Program
#include <stdio.h>#define N 5union product{int pid;char pname[20];char mfd[11];char sname[20];float price;};int main(void){int i;union product p[N];// Input product pricesfor (i = 0; i < N; i++){printf("Enter price of product %d: ", i + 1);scanf("%f", &p[i].price);}// Display product pricesprintf("\nProduct Prices:\n");for (i = 0; i < N; i++){printf("Price of product %d = %.2f\n",i + 1, p[i].price);}return 0;}
Output
Enter price of product 1: 150.50Enter price of product 2: 250.00Enter price of product 3: 99.75Enter price of product 4: 500.00Enter price of product 5: 350.25Product Prices:Price of product 1 = 150.50Price of product 2 = 250.00Price of product 3 = 99.75Price of product 4 = 500.00Price of product 5 = 350.25
Explanation (व्याख्या)
#define N 5defines the number of products.#define N 5products की संख्या निर्धारित करता है।The
productunion contains five members:pid,pname,mfd,snameandprice.productunion में पाँच data members हैं।union product p[N]declares an array of five union variables.यह पाँच union variables का array बनाता है।
The first
forloop accepts the prices of five products.पहला
forloop पाँच products की कीमतें इनपुट करता है।The second
forloop displays the stored product prices.दूसरा
forloop स्टोर की गई product prices प्रदर्शित करता है।
Important Points
All members of a union share the same memory location.
Each union variable can hold one member's value at a time.
This program stores only product prices, not all product details simultaneously.
To store product ID, name, manufacturing date, supplier name and price together, use
structinstead ofunion.
Comments
Post a Comment