• Click below to download Unit 1 programs in pdf file: -




Advance C Practical


Unit 1


/* 1.Write a program to define structure with tag state with fields

state name, number of districts and total population. Read and

display the data. */


#include<stdio.h>

struct state

{

    char name[10];

    int n_dist;

    long population;

};

void main()

{

     struct state s1;


     printf("enter the data of state: name, no of district and population : ");

     scanf("%s%d%d",s1.name,&s1.n_dist,&s1.population);


     printf("The Details is :\n");

     printf("name is %s\n No of dist are %d \n population is %d",s1.name,s1.n_dist,s1.population );

}





/* 2. Write a program to create a list of books details. The details of a

book include title, author, publisher, publishing year, number of

pages, and price. */


#include<stdio.h>


struct book

{

    char book_name[20];

    int publishingyear;

    float book_price;

    char author[15];

    char publisher[15];

    int numberofpages;

};


int main()

{

    struct book b;


        printf("Enter book title: ");

        scanf("%s", b.book_name);


        printf("Enter book author: ");

        scanf("%s", b.author);


        printf("Enter publisher: ");

        scanf("%s", b.publisher);


        printf("Enter publishing year: ");

        scanf("%d", &b.publishingyear);


        printf("Enter number of pages: ");

        scanf("%d", &b.numberofpages);


        printf("Enter book price: ");

        scanf("%f", &b.book_price);



        printf("Book Name: %s\n", b.book_name);

        printf("Book Author: %s\n", b.author);

        printf("Book Id: %s\n", b.publisher);

        printf("Book Id: %d\n", b.publishingyear);

        printf("Book Id: %d\n", b.numberofpages);

        printf("Book price: %f", b.book_price);



}






/* 3. Define a structure called Item with members: Item_code,

Item_name, Price. Create an array of five Items. Create a function

which accepts the Item array and modifies each element with an

increase of 10% in the price. */


#include<stdio.h>

#include<conio.h>


struct item

{

    int code;

    char name[10];

    int price;

};


void main()

{

    struct item itm[5];

    int i;

    for(i=0;i<5;i++)

        {

        printf("enter code, name and price : ");

        scanf("%d%s%d", &itm[i].code,itm[i].name,&itm[i].price);

        }

}


void inc(struct item itm[],int x)

{

        int i;

        for(i=0;i<5;i++)

        {

            itm[i].price += itm[i].price*0.10;

            printf("\n %d %s %d", itm[i].code,itm[i].name,itm[i].price);

        }

}


/* 4. Define a structure to represent a date. Use your structures that

accept two different dates in the format mm dd of the same year.

Write a C program to display the month names of both dates. */


#include<stdio.h>

#include<conio.h>


struct date

{

        int mm;

        int dd;

        int year;

};


void main()

{

        struct date d1,d2;


        printf("Enter first date (mm-dd-yyyy) :");

        scanf("%d%d%d", &d1.mm,&d1.dd,&d1.year);


        printf("\n Enter second date (mm-dd-yyyy) :");

        scanf("%d%d%d", &d2.mm,&d2.dd,&d2.year);


        if(d1.year!=d2.year)

{

         printf("enter date with same year");

}

else

{

        printf("date 1 : %d\n",d1.mm);

        printf("date 2 : %d",d2.mm);

}

}




/* 5. Define a structure that can describe a Hotel. It should have

members that include name, address, grade, room charges, grade

and no of rooms. Write a function to print out all hotel details with

room charges less than a given value. */


#include<stdio.h>


struct hotel

{

    char name[10];

    char add[10]; char grade;

    int roomc;

    int nroom;

};


void main()

{

    struct hotel h1[3];

    int i;


    for(i=0;i<3;i++)

        {

            printf("Enter Name, Address, Grade, Room charges, Number of rooms : ");

            scanf("%s %s %c %d %d", h1[i].name,h1[i].add,&h1[i].grade, &h1[i].roomc,&h1[i].nroom);

        }

}


void printdata(struct hotel h1[],int j)

{

    int ch,i;


    printf("\n Enter room charges");

    scanf("%d", &ch);


    for(i=0;i<j;i++)

    {

        if(h1[i].roomc <ch)

        {

            printf("\n name,add, grade,room charges,number of rooms");

            printf("%s %s %c %d %d", h1[i].name,h1[i].add,h1[i].grade, h1[i].roomc,h1[i].nroom);

        }

    }

}




/* 6. Write a program to accept records of different states using array

of structures. The structure should contain char state and number

of int engineering colleges, int medical colleges, int management

colleges and int universities. Calculate total colleges and display

the state, which is having highest number of colleges. */


#include<stdio.h>

#include<conio.h>


struct sta

{

    char name[10];

    int e;

    int m;

    int mng;

    int uni;

    int t;

};


void main()

{

    struct sta s[3];

    int i,c;


    for(i=0;i<=2;i++)

{

    printf("\n%d)State Name:",i+1);

    scanf("%s",s[i].name);


    printf("Engineering College:");

    scanf("%d",&s[i].e);


    printf("Medical College:");

    scanf("%d",&s[i].m);


    printf("Management College:");

    scanf("%d",&s[i].mng);


    printf("Universities College:");

    scanf("%d",&s[i].uni);

}


       for(i=0;i<=2;i++)

              s[i].t=s[i].e+s[i].m+s[i].mng;


       if(s[0].t>s[1].t>s[2].t)

       {

           printf("\n%s having highset college:%d",s[0].name,s[0].t);

       }

      else if(s[1].t>s[2].t)

      {

          printf("\n%s having highset college:%d",s[1].name,s[1].t);

      }

     else

     {

           printf("\n%s having highset college:%d",s[2].name,s[2].t);

     }

}





/* 7. Define a structure by name time with members seconds, minutes

and hours of int type. A variable of the structure would thus

represent time. If time1 and time2 are two variables of the structure

type, write a program to find the difference of two times using a

function. */


#include<stdio.h>


struct st_time

{

    int sec;

    int min;

    int hr;

};


void main()

{

    struct st_time t1,t2;


    printf("Enter Second : Minute : Hour for the first time : ");

    scanf("%d%d%d", &t1.sec, &t1.min,&t1.hr);


    printf("Enter Second : Minute : Hour for the second time : ");

    scanf("%d%d%d", &t2.sec, &t2.min,&t2.hr);


    diff(t1,t2);

}


void diff(struct st_time t1, struct st_time t2)

  {

    printf("\n Difference between two time is %d %d %d", t1.sec-t2.sec,t1.min-t2.min,t1.hr-t2.hr);

  }





/* 8. Write a program to accept records of different states using array

of structures. The structure should contain char state, int

population, int literacy rate and int per capita income. Assume

suitable data. Display the state whose literacy rate is highest and

whose per capita income is highest. */



# include <stdio.h>

# include <conio.h>


struct data

{

    char state[10];

    long pop;

    int lit_rate;

    int capita;

};


void main()

{

    struct data r[3];

    int i,hl=0,hi=0,x,y;


    for (i=0;i<=2;i++)

    {

        printf ("\nState : "); scanf ("%s",r[i].state);

        printf (" Population : "); scanf ("%ld",&r[i].pop);

        printf (" Literacy rate : "); scanf ("%d",&r[i].lit_rate);

        printf (" Per capita Income : "); scanf ("%d",&r[i].capita);


    if (i>0)

        {

    if (r[i].lit_rate>hl)

        hl=r[i].lit_rate;

        x=i;

            }

    if (r[i].capita>hi)

    {

        hi=r[i].capita;

        y=i;

    }

   else

    {

        hl=r[i].lit_rate;

        hi=r[i].capita;

    }

 }

        printf ("\n State of Highest Literacy Rate ");

        printf ("\nState : %s",r[x].state);

        printf ("\nPopulation : %ld",r[x].pop);

        printf ("\nLiteracy rate : %d",r[x].lit_rate);

        printf ("\nPer capita Income : %d",r[x].capita);

        printf ("\n State of Highest per capita income ");

        printf ("\nState : %s",r[y].state);

        printf ("\nPopulation : %ld",r[y].pop);

        printf ("\nLiteracy rate : %d",r[y].lit_rate);

        printf ("\nPer capita Income : %d",r[y].capita);

}





/* 9. Define a structure employee with members employee name,

basic pay, dearness allowance, house rent, net salary. Declare an

array of 5 employees. Write a function which calculates the net

salary of employees and prints all employee details in descending

order of their net salary. */


#include<stdio.h>


struct employee

{

    char name[10];

    int bpay;

    int da;

    int hra;

    int net_salary;

};


void main()

{

    struct employee e1[5],tmp;

    int i,j;


      for(i=0;i<5;i++)

        {

            printf("\n Enter Employee name, Basic pay, Dearness allowance and House rent of employee : ");

            scanf("%s%d%d%d", e1[i].name,&e1[i].bpay,&e1[i].da,&e1[i].hra);

        }


        for(i=0;i<5;i++)

            {

            for(j=i+1;j<5;j++)

            {

        if(e1[i].net_salary<e1[j].net_salary)

            {

            tmp = e1[i];

            e1[i]=e1[j];

            e1[j]=tmp;

            }

        }

    }


    printf("\n Employee Details in a Descending Order \n\n");


        for(i=0;i<5;i++)

        {

           printf("\n %s \t%d \t%d \t%d \t%d", e1[i].name,e1[i].bpay,e1[i].da,e1[i].hra,e1[i].net_salary);

        }

}


void cal(struct employee e1[],int size)

{

    int i;


    for(i=0;i<size;i++)

    {

      e1[i].net_salary = e1[i].bpay+e1[i].da+e1[i].hra;

    }

}




/* 10. Define a structure with tag population with fields Men and

Women. Create structure with in structure using state and

population structure. Read and display the data. */


#include<stdio.h>

#include<conio.h>


struct state

{

     char name[30];

     long int total;

};

struct population

{

     long int men;

     long int women;

     struct state s1;

}pop;


void main()

{


     printf("Enter name of state : ");

     gets(pop.s1.name);


     printf("Enter Men pop : ");

     scanf("%ld",&pop.men);


     printf("Enter Women pop : ");

     scanf("%ld",&pop.women);


     printf("\nState Name = %s",pop.s1.name);

     printf("\nMen Population = %ld",pop.men);

     printf("\nWomen Population = %ld",pop.women);

}