C Programming code for Books (Search,Sorting,palindrome) data Structure

Write a Modular C Programming code for Getting Books Sorting, Linear Search, palindrome in data Structure

Read the number of students, read their information, and display the details of the students:

CODE:

#include<stdio.h>
struct book
{
    char name[30];
    int isbn;
    int no_of_pages;
    float price;
};
struct book b[1000];
void readStructure(struct book b[1000] ,int n);
void displayStructure(struct book b[1000] ,int n);
void orderBooks(struct book b[1000] ,int n);
void searchBook(struct book b[1000] , int n);
void palindromeISBN(struct book b[1000], int n);
int isPalindrome(int k);
main()
{
    int n;
    int k;
    printf("Enter the number of books: ");
    scanf("%d",&n);
    readStructure(b,n);
    displayStructure(b,n);
    orderBooks(b,n);
    searchBook(b,n);
    palindromeISBN(b,n);
}
void readStructure(struct book b[1000] ,int n)
{
    int i;
    for(i=0;i<n;i++)
    {
        printf("\nEnter Details of Book %d",i+1);
        printf("\nEnter the Name: ");
scanf("%s",&b[i].name);
printf("Enter the IsBn: ");
scanf("%d",&b[i].isbn);
printf("Enter the No of Pages: ");
scanf("%d",&b[i].no_of_pages);
printf("Enter the Price: ");
scanf("%f",&b[i].price);
printf("\n+++++++++++++++++++++++++++++++++++++++++++++++++\n");
    }
}
void displayStructure(struct book b[1000] ,int n)
{
    int i,k=1;
    printf("Book details\n");
    for(i=0;i<n;i++)
    {
         printf("\nDetails of Book %d",i+1);
        printf("\nName: %s",b[i].name);
printf("\nEnter the IsBn: %d",b[i].isbn);
printf("\nEnter the No of Pages: %d",b[i].no_of_pages);
printf("\nEnter the Price: %f",b[i].price);
printf("\n+++++++++++++++++++++++++++++++++++++++++++++++++\n");

    }

}
void orderBooks(struct book b[1000] ,int n)
{
    int i,j;
    struct book temp;
    printf("\nAfter arranging the books in ascending order\n");
    for(i=0;i<n-1;i++)
    {
         for(j=0;j<n-i-1;j++)
 { 
if(b[j].isbn>b[j+1].isbn)
            {
                temp=b[j];
                b[j]=b[j+1];
                b[j+1]=temp;
            }
        }
    }
    displayStructure(b,n);
   printf("\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
}
void searchBook(struct book b[1000] , int n)
{
    int i,k,j;
    int flag=1;
    printf("Enter the isbn number of book to search: ");
    scanf("%d",&k);
    printf("Details of Book of required ISBN number is: \n");
    for(i=0;i<n;i++)
    {
            if(b[i].isbn==k)
        {
             printf("Book found\n");
             printf("Name= %s\nISBN= %d\nNumber of pages=%d\nPrice= %f\n",b[i].name,b[i].isbn,b[i].no_of_pages,b[i].price);
             flag=1;
             break;
        }
        else
            flag=0;
    }
    if(flag==0)
        printf("\nBook not found!!!");
        printf("-----------------------------------------------------------------------------------------------------------\n");
}
void palindromeISBN(struct book b[1000], int n)
{
    int i,k;
    printf("\nDetails of books whose ISBN is a palindrome are: \n");
    printf("\n\nName\tISBN\tNumber of pages\tPrice\n");
    for (i=0;i<n;i++)
    {
        k=isPalindrome(b[i].isbn);
        if(k==1)
            printf("%s\t\t%d\t\t%d\t\t%f\n",b[i].name,b[i].isbn,b[i].no_of_pages,b[i].price);
    }
    printf("-------------------------------------------------------------------------------------------");
}
int isPalindrome(int k)
{
    int r,rev=0;
    int temp;
    temp=k;
    while(k)
    {
        r=k%10;
        rev=rev*10+r;
        k=k/10;
    }
    if(temp==rev)
        return 1;
    else
        return 0;
}


OUTPUT

Enter the number of books: 5

Enter Details of Book 1
Enter the Name: DavinciCode
Enter the IsBn: 101
Enter the No of Pages: 500
Enter the Price: 600

+++++++++++++++++++++++++++++++++++++++++++++++++

Enter Details of Book 2
Enter the Name: Avathar
Enter the IsBn: 123321
Enter the No of Pages: 600
Enter the Price: 800

+++++++++++++++++++++++++++++++++++++++++++++++++

Enter Details of Book 3
Enter the Name: Avanish
Enter the IsBn: 606
Enter the No of Pages: 800
Enter the Price: 500

+++++++++++++++++++++++++++++++++++++++++++++++++

Enter Details of Book 4
Enter the Name: Nagas
Enter the IsBn: 909
Enter the No of Pages: 650
Enter the Price: 850

+++++++++++++++++++++++++++++++++++++++++++++++++

Enter Details of Book 5
Enter the Name: Papillon
Enter the IsBn: 424
Enter the No of Pages: 1800
Enter the Price: 5000

+++++++++++++++++++++++++++++++++++++++++++++++++
Book details

Details of Book 1
Name: DavinciCode
Enter the IsBn: 101
Enter the No of Pages: 500
Enter the Price: 600.000000
+++++++++++++++++++++++++++++++++++++++++++++++++

Details of Book 2
Name: Avathar
Enter the IsBn: 123321
Enter the No of Pages: 600
Enter the Price: 800.000000
+++++++++++++++++++++++++++++++++++++++++++++++++

Details of Book 3
Name: Avanish
Enter the IsBn: 606
Enter the No of Pages: 800
Enter the Price: 500.000000
+++++++++++++++++++++++++++++++++++++++++++++++++

Details of Book 4
Name: Nagas
Enter the IsBn: 909
Enter the No of Pages: 650
Enter the Price: 850.000000
+++++++++++++++++++++++++++++++++++++++++++++++++

Details of Book 5
Name: Papillon
Enter the IsBn: 424
Enter the No of Pages: 1800
Enter the Price: 5000.000000
+++++++++++++++++++++++++++++++++++++++++++++++++

After arranging the books in ascending order
Book details

Details of Book 1
Name: DavinciCode
Enter the IsBn: 101
Enter the No of Pages: 500
Enter the Price: 600.000000
+++++++++++++++++++++++++++++++++++++++++++++++++

Details of Book 2
Name: Papillon
Enter the IsBn: 424
Enter the No of Pages: 1800
Enter the Price: 5000.000000
+++++++++++++++++++++++++++++++++++++++++++++++++

Details of Book 3
Name: Avanish
Enter the IsBn: 606
Enter the No of Pages: 800
Enter the Price: 500.000000
+++++++++++++++++++++++++++++++++++++++++++++++++

Details of Book 4
Name: Nagas
Enter the IsBn: 909
Enter the No of Pages: 650
Enter the Price: 850.000000
+++++++++++++++++++++++++++++++++++++++++++++++++

Details of Book 5
Name: Avathar
Enter the IsBn: 123321
Enter the No of Pages: 600
Enter the Price: 800.000000
+++++++++++++++++++++++++++++++++++++++++++++++++

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Enter the isbn number of book to search: 424
Details of Book of required ISBN number is:
Book found
Name= Papillon
ISBN= 424
Number of pages=1800
Price= 5000.000000
-----------------------------------------------------------------------------------------------------------

Details of books whose ISBN is a palindrome are:


Name            ISBN    Number of pages     Price
DavinciCode     101           500         600.000000
Papillon        424           1800        5000.000000
Avanish         606           800         500.000000
Nagas           909           650         850.000000
Avathar         123321        600         800.000000
-------------------------------------------------------------------------------------------
Process returned 0 (0x0)   execution time : 1135.032 s
Press any key to continue.

 

Please find some more codes of 1D Arrays, 2D Arrays, Pointers on the below page:

Top 100+ C Programming codes – KLE Technological University

 







Leave a Comment

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

Welcome to FactsPrime

Sorry, We have detected that you have activated Ad-Blocker. Please Consider supporting us by disabling your Ad Blocker, It helps us in maintaining this website. To View the content, Please disable adblocker and refresh the page.

Thank You !!!