Program to search particular element in structure


C File =>



/* Write a C program to accept details of n items (code, name, price) using structure. Perform the following operations:
a. Display all items having price > ___
b. Search for an item by name.  */


#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef struct item
{
    int code;
    char name[30];
    float price;
};
int main()
{
    struct item p[50];
    int n,i,ch;
    char key_name[30];
    printf("\n Enter how many items? ");
    scanf("%d",&n);
    for(i=0;i<n;i++)
   {
     printf("\n\n Enter item code%d ",i+1);
     scanf("%d",&p[i].code);
     printf("\n Enter item name%d",i+1);
     scanf("%s",p[i].name);
     printf("\n Enter price%d",i+1);
     scanf("%f",&p[i].price);
    }
    printf("\n\n 1. Display all items having price greater than 250\n 2. Search item
    scanf("%d",&ch);
do
 {
    switch(ch)
   {
     case 1: printf("\n\n Details of Items are:");
                 for(i=0;i<n;i++)
                {
                  if(p[i].price>250)
                 {
                   printf("\n\n Item code : %d",p[i].code);
                   printf("\n Item name : %s",p[i].name);
                   printf("\n Item price : %f",p[i].price);
                 }
               }
     break;
     case 2: printf("\n Enter name to search : ");
                 scanf("%s",key_name);
                 for(i=0;i<n;i++)
                {
                  if(strcmp(p[i].name,key_name)==0)
                 {
                        printf("\n\n Details of Item are :");
                        printf("\n Item code : %d",p[i].code);
                        printf(" \n Item name : %s",p[i].name);
                        printf("\n Item Price : %f",p[i].price);
                   }
                 }
      break;
      case 3: exit(0);
      break;
      default : printf("\n Invalid input...");
     }
 }while(ch!=3);
return 0;
}


Comments

Popular posts from this blog

Write a program to allocate memory dynamically for n integers. Accept the elements and calculate their sum and average

C program to accept names of n cities and search for city named “Pune”.