Sunday, September 14, 2014

create a menu driven library management program using structure and functions

0 comments

Problem

Write a menu driven program that depicts the working of a library. The menu options should be:
  1. Add book information
  2. Display book information
  3. List all books of given author
  4. List the title of specified book
  5. List the count of books in the library
  6. List the books in the order of accession number
  7. Exit
Create a structure called library to hold accession number, title of the book, author name, price of the book, and flag indicating whether book is issued or not. Create an array of size 25 of type library. Input record from user and check all the options of the program.

Solution - Code

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

void add_book();
void disp_book();
void book_auth();
void count_book();

struct lib
{
 int acc_no;
 char book_title[20];
 char author[20];
 int cost;
}b[100];
int count;
main()
{
 int ch;
 while(1)
 {
  printf("\n 1:enter the book info\n");
  printf("\n 2:display book info\n");
  printf("\n 3:book author\n");
  printf("\n 4:count book\n");
  printf("\n 5:exit\n");
  printf("\n enter the choice \n");
  scanf("%d",&ch);
  switch(ch)
  {
   case 1:
    add_book();
    getch();
    break;
   case 2:
    disp_book();
    getch();
    break;
   case 3:
    book_auth();
    getch();
    break;
   case 4:
    count_book();
    getch();
    break;
   case 5:
    exit(0);
  }
 }
}
//-----------------------------------
void add_book()
{
 if(count==9)
 {
  printf("\n no more space\n");
  return;
 }
 printf("\n enter the detail of book \n");
 printf("\n enter accession number of book =");
 scanf("%d",&b[count].acc_no);
 fflush(stdin);
 printf("\n enter the book title=");
 gets(b[count].book_title);
 fflush(stdin);
 printf("\n enter the name of author=");
 gets(b[count].author);
 fflush(stdin);
 printf("\n enter the cost of book=");
 scanf("%d",&b[count].cost);
 count++;
}
//***********************
void disp_book()
{
 int i;
 printf("\n detail of %d booksin library",count);
 for(i=0;i<count;i++)
 {
  printf("\n %d\n%s\n%s\n%d",b[i].acc_no,b[i].book_title,b[i].author,b[i].cost);
 }
}
//*********************
void book_auth()
{
 int i,cnt=0;
 char name[20];
 printf("\n enter the name of author=");
 gets(name);
 for(i=0;i<count;i++)
 {
  if(strcmp(name,b[i].author)==0)
  {
   cnt++;
   printf("\n %d\n%s\n%s\n%d",b[i].acc_no,b[i].book_title,b[i].author,b[i].cost);
  }
 }
 if(cnt==0)
  printf("\n no such book \n");
}
//*******************************
void count_book()
{
 printf("\n total no of book in library =%d",count);
}

Sample Output

add book record c language

display library books

display total library books in c language

Read More..

compare two given dates using structure

2 comments

Problem

Write a program that compares two given dates. To store date use structure say date that contains three members namely date, month, and year. If the dates are equal then display message as "Equal" otherwise "Unequal".

Solution - Code

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
struct date
{
 int date;
 int month;
 int year;
};
int main ()
{
 int i,f=0;  struct date d[2];  for(i=0;i<2;i++)
 {
  printf("\nEnter day for the %dth date\n",i+1);
  scanf("%d",&d[i].date);

  printf("\nEnter the month for the %dth date\n",i+1);
  scanf("%d",&d[i].month);

  printf("\nEnter the year for the %dth date\n",i+1);
  scanf("%d",&d[i].year);
 }
 if(d[0].date==d[1].date)
 {
  if(d[0].month==d[1].month)
  {
   if(d[0].year==d[1].year)
   {
    f=1;
   }
  }
 }
 if(f==1)
  printf("\nThe dates are equal");
 else
  printf("\nThe dates are not equal");

 printf ("\n\npress any key to close.");
 getch();
}

Sample Output

compare dates using structure

Read More..

write a program that allows the user to input data and search

0 comments

Problem

Create a structure to specify data on students given below:
Roll number, Name, Department, Course, Year of joining
Create an array of structure. Use a loop to input values and then display those values. Also write a function:
void searchstudent (int roll_no);
This function will search a student record from that array whose Roll number is same as roll_no and display that record otherwise it displays record not found.

Solution - Code

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
void searchstudent (int roll_no);
struct book
{
 int roll;
 char name[20];
 char department[20];
 char course[20];
 int year;
}b[2];
int main ()
{
 int i, roll_no;

 for (i=0;i<2;i++)
 {
  printf ("Enter roll no. : ");
  scanf ("%d",&b[i].roll);

  fflush (stdin);
  printf ("Enter name: ");
  gets (b[i].name);

  fflush (stdin);
  printf ("Enter department: ");
  gets (b[i].department);

  fflush (stdin);
  printf ("Enter course: ");
  gets (b[i].course);

  printf ("Enter year: ");
  scanf ("%d",&b[i].year);
 }

 for (i=0;i<2;i++)
  printf("\n\nRoll number: %d\nName: %s\nDepartment: %s\nCourse: %s\nYear: %d\n",b[i].roll,b[i].name,b[i].department,b[i].course,b[i].year);

 printf ("SEARCH DATA(enter roll number): ");
 scanf ("%d",&roll_no);

 searchstudent(roll_no);

 printf ("\npress any key to close.");
 getch();
}
void searchstudent (int roll_no)
{
 int i;

 for (i=0;i<2;i++)
 {
  if (roll_no == b[i].roll)
   printf("\n\nRoll number: %d\nName: %s\nDepartment: %s\nCourse: %s\nYear: %d\n",b[i].roll,b[i].name,b[i].department,b[i].course,b[i].year);
 }
}

Sample Output

search of a student using structure

Read More..

create an array of structure to display user input values

0 comments

Problem

Create a structure to specify data on students given below:
  • Roll number
  • Name
  • Department
  • Course
  • Year of joining
Create an array of that structure. Use a loop to input values and then display those values.

Solution - Code

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
struct book
{
int roll;
char name[20];
char department[20];
char course[20];
int year;
}b[2];
int main ()
{
 int i;

 for (i=0;i<2;i++)
 {
  printf ("Enter roll no. : ");
  scanf ("%d",&b[i].roll);

  fflush (stdin);
  printf ("Enter name: ");
  gets (b[i].name);

  fflush (stdin);
  printf ("Enter department: ");
  gets (b[i].department);

  fflush (stdin);
  printf ("Enter course: ");
  gets (b[i].course);

  printf ("Enter year: ");
  scanf ("%d",&b[i].year);
 }

 for (i=0;i<2;i++)
  printf("\n\nRoll number: %d\nName: %s\nDepartment: %s\nCourse: %s\nYear: %d\n",b[i].roll,b[i].name,b[i].department,b[i].course,b[i].year);

 printf ("\npress any key to close.");
 getch();
}

Sample Output

using array of structure to display user input data

Read More..

Create a structure that stores student data and then display it

0 comments

Problem

Create a structure to specify data on students given below:
  • Roll number
  • Name
  • Department
  • Course
  • Year of joining
Create a variable of that structure, input some value from user and then display those values.

Solution - Code

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include <string.h>
struct book
{
 int roll;
 char name[20];
 char department[20];
 char course[20];
 int year;
}b;
int main ()
{
 printf ("Enter roll no. : ");
 scanf ("%d",&b.roll);

 fflush (stdin);
 printf ("Enter name: ");
 gets (b.name);

 fflush (stdin);
 printf ("Enter department: ");
 gets (b.department);

 fflush (stdin);
 printf ("Enter course: ");
 gets (b.course);

 printf ("Enter year: ");
 scanf ("%d",&b.year);

 printf("\nRoll number: %d\nName: %s\nDepartment: %s\nCourse: %s\nYear: %d\n",b.roll,b.name,b.department,b.course,b.year);

 printf ("\npress any key to close.");
 getch();
}

Sample Output

display user input data using structure

Read More..

Display the user input names by allocating memory dynamically (malloc function)

0 comments

Problem

Write a program that asks the user to input name of students and then displays these names on computer screen. You have to write a program such that it should work with any numbers of names. e.g. users can input 6 names or 100 names.
  1. For such problems declare an array of pointers to characters.
  2. Input a name from user in a temp array of size 50 using gets( ) function. Then allocate a dynamic memory to store that name and assign the base address of dynamic memory to the first index of array of pointers.   
  3. Do the processes in b again and again until the user input an empty string.
  4.  Then displays all the names using array of pointers

Solution - Code

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include <string.h>
int main ()
{
 char *name[30];
 char temp[30];
 int i, k;

 printf ("Enter names: ");

 for (i=0;i<1000;i++)
 {
  fflush(stdin);

  gets (temp);

  if (strlen(temp) != 0)
  {
   name[i] = (char *) malloc (strlen(temp)+1);
   strcpy (name[i],temp);
  }
  else
   break;
 }

 printf ("\n\nNames that you have entered are: ");

 for (k=0;k<i;k++)
 {
  printf ("\n%s",name[k]);
 }

 printf ("\n\npress any key to close.");
 getch();
}

Sample Output

dynamic memory allocating using malloc function

Read More..

write a program to calculate sum and average by allocating dynamic memory (malloc function)

0 comments

Problem

Write a program that asks the user to input marks of some subjects. The program calculates sum and average of those marks and prints them on screen. As a programmer you are not sure that a user wants to input marks of how many subjects. So, your program first asks the user for how many subject you want to input marks. The program allocates dynamic memory using malloc function.
Prototype of malloc function is:
void* malloc (int n);

where n is the number of bytes to be allocated you have to type cast void* pointer to int* since you know that you will be storing int values in that allocated memory.

Note: Use free function when the program ends.

Solution - Code

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include <string.h>
int main ()
{
 int sum=0, avg, i;
 int how_many;
 int *p;

 printf ("Enter number of subjects whose marks you want to input: ");
 scanf ("%d",&how_many);

 p = (int *)malloc(how_many * 4);

 if (p == NULL)
 {
  printf(" Out of memory!\n");
  printf ("Press any key to close.");
  exit(0);
 }

 for (i=0;i<how_many;i++)
 {
  printf ("Enter marks of %dth subject: ",i+1);
  scanf ("%d",&p[i]);

  sum = (sum + p[i]);
 }

 avg = (sum / how_many);

 printf ("Sum = %d\n\n",sum);
 printf ("Average = %d\n\n",avg);

 free (p);

 printf ("press any key to close.");
 getch();
}

Sample Output


calculate sum & average using malloc function

Read More..

Copyright 2017. All Rights Reserved. Privacy Policy / Terms And Conditions / Sitemap / Contact