0% found this document useful (0 votes)
6 views25 pages

Unit Pr1

The document contains multiple C programs that define various structures such as state, book, item, hotel, and employee, along with their respective fields. Each program includes functionality to read, display, and manipulate data related to the defined structures, such as calculating total colleges, finding the difference between times, and sorting employee salaries. The programs demonstrate the use of arrays, functions, and basic input/output operations in C.

Uploaded by

Kathan Patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views25 pages

Unit Pr1

The document contains multiple C programs that define various structures such as state, book, item, hotel, and employee, along with their respective fields. Each program includes functionality to read, display, and manipulate data related to the defined structures, such as calculating total colleges, finding the difference between times, and sorting employee salaries. The programs demonstrate the use of arrays, functions, and basic input/output operations in C.

Uploaded by

Kathan Patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 25

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>

# include <conio.h>

struct state

char *name;

int dist;

long pop;

};

void main()

clrscr();

state s;

printf ("\n Enter state name, number of dist. and population : ");

scanf ("%s %d %ld",s.name,&s.dist,&s.pop);

printf ("\n State : %s",s.name);

printf ("\n District : %d",s.dist);

printf ("\n Population : %ld",s.pop);

getch();

}
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>

#include<conio.h>

#define size 10

struct bk

char *ti[20];

char *author [20];

char *pub[20];

int py;

int page;

int pr;

};

void main()

struct bk b[size];

int i=0,n;

clrscr();

printf("Enter Book Details Number \n");

scanf("%d",&n);

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

printf("Enter Title : \n");

scanf("%s",&b[i].ti);
printf("Enter Name of Author : \n");

scanf("%s",&b[i].author);

printf("Enter Name of Publisher : \n");

scanf("%s",&b[i].pub);

printf("Enter Publishing Year : \n");

scanf("%d",&b[i].py);

printf("Enter Pages : \n");

scanf("%d",&b[i].page);

printf("Enter Book Price :\n");

scanf("%d",&b[i].pr);

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

printf("%s\t",b[i].ti);

printf("%s\t",b[i].author);

printf("%s\t",b[i].pub);

printf("%d\t",b[i].py);

printf("%d\t",b[i].pr);

getch();

}
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 date

int date,month,year;

};

void month_name(struct date d[2])

int i;

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

printf("\n Name of the Month %d is :",i+1);

if(d[i].month == 1)

printf("January");

else if(d[i].month == 2)

printf("February");

else if(d[i].month == 3)

{
printf("March");

else if(d[i].month == 4)

printf("April");

else if(d[i].month == 5)

printf("May");

else if(d[i].month == 6)

printf("June");

else if(d[i].month == 7)

printf("July");

else if(d[i].month == 8)

printf("August");

else if(d[i].month == 9)

printf("Sepember");
}

else if(d[i].month == 10)

printf("October");

else if(d[i].month == 11)

printf("November");

else if(d[i].month == 12)

printf("December");

else

printf("Unvalide Month");

void main()

int i;

struct date d[2];

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

printf("===== Enter Date %d=====",i+1);

printf("\n Enter Day :");

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

printf("\n Enter Month :");

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

printf("\n Enter Year :");

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

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

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

printf("%d \ %d \ %d.",d[i].date,d[i].month,d[i].year);

// month_name(d);

getch();

}
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 date,month,year;

};

void month_name(struct date d[2])

int i;

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

printf("\n Name of the Month %d is :",i+1);

if(d[i].month == 1)

printf("January");

else if(d[i].month == 2)

printf("February");

else if(d[i].month == 3)

{
printf("March");

else if(d[i].month == 4)

printf("April");

else if(d[i].month == 5)

printf("May");

else if(d[i].month == 6)

printf("June");

else if(d[i].month == 7)

printf("July");

else if(d[i].month == 8)

printf("August");

else if(d[i].month == 9)

printf("Sepember");
}

else if(d[i].month == 10)

printf("October");

else if(d[i].month == 11)

printf("November");

else if(d[i].month == 12)

printf("December");

else

printf("Unvalide Month");

void main()

int i;

struct date d[2];

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

printf("===== Enter Date %d=====",i+1);

printf("\n Enter Day :");

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

printf("\n Enter Month :");

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

printf("\n Enter Year :");

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

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

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

printf("%d \ %d \ %d.",d[i].date,d[i].month,d[i].year);

month_name(d);

getch();

}
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>

#include<conio.h>

struct h

char name[30];

char add[20];

int gra;

int chr;

int room;

};

void hotel();

struct h s[]={

{"South Point Hert","Maxico",3,300,167},

{"Hotel Moon Rocket","Los Angels",2,500,150},

{"Tropicana Casino & Resort","Los Vegas",3,100,50},

{"Rajavir Palace & Resort","India",4,100,50},

};

void main()

{
clrscr();

int i;

char c;

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

printf("\nHOTEL NAME:%s",s[i].name);

printf("\nADDRESH:%s",s[i].add);

printf("\nGRADE:%d",s[i].gra);

printf("\nCHARGES:%d",s[i].chr);

printf("\nNO.OF ROOM:%d\n",s[i].room);

hotel();

getch();

void hotel()

int i,k;

char c;

printf("\nSHOW HOTEL ROOM CHRGES LEE THAN:");

scanf("%d",k);

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

{
if(s[i].chr<=k)

printf("\n %d.HOTEL NAME:%s",i+1,s[i].name);

printf("\nADDRESH:%s",s[i].add);

printf("\nGRADE:%d",s[i].gra);

printf("\nCHARGES:%d",s[i].chr);

printf("\nNO.OF ROOM:%d\n",s[i].room);

getch();

}
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[15];

int e,m,mng,uni,t;

};

void main()

struct sta s[3];

int i,c;

clrscr();

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

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

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

printf("Engneering 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);

printf("-----------------------");

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[0].t > s[2].t)

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

else if(s[1].t > s[0].t && 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);

getch();

}
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>

#include <conio.h>

struct TIME {

int seconds;

int minutes;

int hours;

};

void differenceBetweenTimePeriod(struct TIME t1,

struct TIME t2,

struct TIME *diff);

int main()

struct TIME startTime, stopTime, diff;

printf("Enter the start time. \n");

printf("Enter hours, minutes and seconds: ");

scanf("%d %d %d", &startTime.hours,&startTime.minutes,&startTime.seconds);

printf("Enter the stop time. \n");

printf("Enter hours, minutes and seconds: ");

scanf("%d %d %d", &stopTime.hours,&stopTime.minutes,&stopTime.seconds);

// Difference between start and stop time


differenceBetweenTimePeriod(startTime, stopTime, &diff);

printf("\nTime Difference: %d:%d:%d - ", startTime.hours,startTime.minutes,startTime.seconds);

printf("%d:%d:%d ", stopTime.hours,stopTime.minutes,stopTime.seconds);

printf("= %d:%d:%d\n", diff.hours,diff.minutes,diff.seconds);

getch();

return 0;

// Computes difference between time periods

void differenceBetweenTimePeriod(struct TIME start,struct TIME stop,struct TIME *diff)

while (stop.seconds > start.seconds)

--start.minutes;

start.seconds += 60;

diff->seconds = start.seconds - stop.seconds;

while (stop.minutes > start.minutes)

--start.hours;

start.minutes += 60;

}
diff->minutes = start.minutes - stop.minutes;

diff->hours = start.hours - stop.hours;

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 state

char name[15];

int pop,lit,inc;

};

void main()

struct state s[3];

int i;

clrscr();

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


{

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

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

printf("Population :");

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

printf("Literacy Rate ( Out of 100% ) :");

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

printf("Per Capita Income : Rs.");

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

printf("\n\n");

if (s[0].lit > s[1].lit && s[0].lit > s[2].lit)

printf("\n %s is Highest in Literacy",s[0].name);

else if(s[1].lit > s[0].lit && s[1].lit > s[2].lit)

printf("\n %s is Highest in Literacy",s[1].name);

else

printf("\n %s is Highest in Literacy",s[2].name);


}

if (s[0].inc > s[1].inc && s[0].inc > s[2].inc)

printf("\n %s is Highest in Income",s[0].name);

else if(s[1].inc > s[0].inc && s[1].inc > s[2].inc)

printf("\n %s is Highest in Income",s[1].name);

else

printf("\n %s is Highest in Income",s[2].name);

getch();

}
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>

#include <conio.h>

struct emp

char name[20];

int p,da,hr,ns;

};

void main()

struct emp e[5];

int i,t=0;

clrscr();

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

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

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

printf("Basic Pay : ");

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

printf("Dearness Allowance : ");


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

printf("House Rent : ");

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

printf("Net Salary : ");

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

printf("\n");

printf("\n");

void tl();

void tl(int ,int );

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

t = t + e[i].ns;

printf("\n\n Net Salary : %d.",t);

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

printf("\nEmployee Net Salary : %d.\n\n",e[i].ns);

printf("\nEmployee House Rent : %d.",e[i].hr);

printf("\nEmployee Dearness Allowance : %d.",e[i].da);


printf("\nEmployee Basic Pay : %d.",e[i].p);

printf("\t\t %d) Employee Name : %s.",i+1,e[i].name);

getch();

}
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 <conio.h>

#include<stdio.h>

struct population

int nw;

int nm;

};

struct state

char name[10];

struct population p1;

};

void main()

struct state s1;

printf("\n enter data of state: name, no of women and men");

scanf("%s%d%d",s1.name,&s1.p1.nw,&s1.p1.nm);

printf("\n The Details are as under\n");

printf("\n name is %s\n No of women are %d \n Men are %d",s1.name,s1.p1.nw,s1.p1.nm);

You might also like