[19/12, 12:50 pm] Rajadurai: #include <stdio.h>
#include <string.h>
struct Inventory
{
int id;
char name[30];
int quantity;
float price;
};
int main()
{
struct Inventory item[10];
int n, i, choice;
int found;
int addQty, sellQty;
char searchItem[60];
printf("Enter number of items: ");
scanf("%d", &n);
/* Input inventory details */
for(i = 0; i < n; i++)
{
printf("ID: ");
scanf("%d", &item[i].id);
printf("Name: ");
scanf("%s", item[i].name);
printf("Quantity: ");
scanf("%d", &item[i].quantity);
printf("Price: ");
scanf("%f", &item[i].price);
}
do
{
printf("--- Inventory Menu ---");
printf("\n1. View Items");
printf("\n2. Add Stock");
printf("\n3. Sell Item");
printf("\n4. Exit");
printf("\nEnter your choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("\nID\tName\tQuantity\tPrice\n");
for(i = 0; i < n; i++)
{
printf("%d\t%s\t%d\t\t%.2f\n",
item[i].id,
item[i].name,
item[i].quantity,
item[i].price);
}
break;
case 2:
printf("Enter item name to add stock: ");
scanf("%s", searchItem);
found = 0;
for(i = 0; i < n; i++)
{
if(strcmp(item[i].name,searchItem)==0)
{
found=1;
printf("Enter quantity to add: ");
scanf("%d", &addQty);
item[i].quantity += addQty;
printf("Stock added successfully.\n");
printf("Available quantity =%d\n",item[i]. quantity);
break;
}
}
if(found == 0)
printf("Item not found.\n");
break;
case 3:
printf("Enter item name to sell: ");
scanf("%s", searchItem);
for(i = 0; i < n; i++)
{
if(strcmp(item[i].name,searchItem)==0)
{
printf("Enter quantity to sell: ");
scanf("%d", &sellQty);
if(item[i].quantity >= sellQty)
{
item[i].quantity -= sellQty;
printf("Item sold successfully.\n");
printf("Available quantity =%d\n",item[i]. quantity);
}
else
{
printf("Insufficient stock.\n");
}
}
else
printf("Item not found.\n");
}
break;
case 4:
printf("Exiting program...\n");
break;
default:
printf("Invalid choice. Try again.\n");
}
} while(choice != 4);
return 0;
}
[19/12, 12:53 pm] Rajadurai: #include <stdio.h>
#include <string.h>
struct Book {
int id;
char title[50];
char author[50];
char publisher[50];
int copies;
};
int main() {
int choice, i, found;
char searchTitle[50];
struct Book lib[3] = {
{101, "CProgramming", "Dennis", "PHI", 5},
{102, "DataStructures", "Seymour", "McGraw", 3},
{103, "OperatingSystems", "Silberschatz", "Wiley", 2}
};
int n = 3;
do {
printf("\n--- MENU ---\n");
printf("1. Display All Books\n");
printf("2. Issue a Book\n");
printf("3. Return a Book\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
// -------- DISPLAY BOOKS --------
case 1:
printf("\nID\tTitle\t\tAuthor\t\tPublisher\tCopies\n");
for (i = 0; i < n; i++) {
printf("%d\t%-15s %-15s %-15s %d\n",
lib[i].id,
lib[i].title,
lib[i].author,
lib[i].publisher,
lib[i].copies);
}
break;
// -------- ISSUE BOOK --------
case 2:
found = 0;
printf("Enter title to issue: ");
scanf("%s", searchTitle);
for (i = 0; i < n; i++) {
if (strcmp(lib[i].title, searchTitle) == 0) {
found = 1;
if (lib[i].copies > 0) {
lib[i].copies--;
printf("Book issued successfully.\n");
} else {
printf("Sorry! No copies available.\n");
}
break;
}
}
if (!found) {
printf("Book not found.\n");
}
break;
// -------- RETURN BOOK --------
case 3:
found = 0;
printf("Enter title to return: ");
scanf("%s", searchTitle);
for (i = 0; i < n; i++) {
if (strcmp(lib[i].title, searchTitle) == 0) {
lib[i].copies++;
found = 1;
printf("Book returned successfully.\n");
break;
}
}
if (!found) {
printf("Book not found.\n");
}
break;
// -------- EXIT --------
case 4:
printf("Exiting program...\n");
break;
default:
printf("Invalid choice!\n");
}
} while (choice != 4);
return 0;
}