ACP UNIT-1 programs + files

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. 
ANS)

#include<stdio.h>
#include<conio.h>
void main()
{
struct state
{
char nam[20];
long int  dis,pop;
}state;
clrscr();
printf("Enter Your state name = ");
gets(state.nam);
printf("Enter no of districts = ");
scanf("%d",&state.dis);
printf("Enter total population = ");
scanf("%ld",&state.pop);

printf("\n\nState name = %s",state.nam);
printf("\n\nTotal District = %d",state.dis);
printf("\n\nPopulation = %ld",state.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.
ASN)

#include<stdio.h>
#include<conio.h>
void main()
{
struct book
{
char title[20],b_name[20],a_name[30],p_name[40];
int  year,price,page;
}book;
clrscr();
printf("Enter Book name = ");
gets(book.b_name);
printf("Enter Title = ");
gets(book.title);
printf("Enter Author name = ");
gets(book.a_name);
printf("Enter Publisher name = ");
gets(book.p_name);
printf("Enter Publishing Year = ");
scanf("%d",&book.year);
printf("Enter total num of pages = ");
scanf("%d",&book.page);
printf("Enter book price = ");
scanf("%d",&book.price);

printf("\nBook name is = %s \nTitle is = %s \nAuthor name is = %s \nPublisher name is = %s",book.b_name,book.title,book.a_name,book.p_name);
printf("\nPublishing year is = %d \nTotal page is = %d \nPrice = %d",book.year,book.page,book.price);

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.
ANS)

#include<stdio.h>
#include<conio.h>
int i=0;
struct item
{
char item_name[30];
int  item_code,price;
}item[3];
void incre(struct item[]);

void main()
{

clrscr();
for(i=0;i<=2;i++)
{
printf("Enter %d item code = ",i+1); flushall();
scanf("%d",&item[i].item_code);    flushall();
printf("\nEnter %d item name = ",i+1);   flushall();
gets(item[i].item_name);           flushall();
printf("\nEnter %d item Price = ",i+1);  flushall();
scanf("%d",&item[i].price);
clrscr();
}
incre(item);
for(i=0;i<=2;i++)
{
printf("\n%d item code = %d",i+1,item[i].item_code);
printf("\n%d item name = %s",i+1,item[i].item_name);
printf("\n%d item price = %d \n\n--------------------------",i+1,item[i].price);
}
getch();
}

void incre(struct item item[])
{
for(i=0;i<=2;i++)
{
item[i].price=item[i].price + (item[i].price*10/100);
}
}


4) 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.
ANS)

#include<stdio.h>
#include<conio.h>
void main()
{
struct date
{
int  dd,mm,yy;
}date1,date2;
clrscr();
printf("Enter year for both dates = ");
scanf("%d",&date1.yy);
printf("\nEnter 1st Date (dd\mm) = ");  flushall();
scanf("%d %d",&date1.dd,&date1.mm); flushall();
printf("\nEnter 2nd Date (dd\mm\yyyy) = "); flushall();
scanf("%d %d",&date2.dd,&date2.mm); flushall();

printf("\nDate 1 = %d/%d/%d",date1.dd,date1.mm,date1.yy);
printf("\nDate 2 = %d/%d/%d ",date2.dd,date2.mm,date1.yy);
printf("\n!st date month name is = ");
switch(date1.mm)
{
case 1 : printf("Jan"); break;
case 2 : printf("Feb"); break;
case 3 : printf("Mar"); break;
case 4 : printf("Apr"); break;
case 5 : printf("may"); break;
case 6 : printf("Jun"); break;
case 7 : printf("July"); break;
case 8 : printf("Aug"); break;
case 9 : printf("spt"); break;
case 10 : printf("oct"); break;
case 11 : printf("nov"); break;
case 12 : printf("Dec"); break;
default : printf("Invalid no");
}
printf("\n2nd date month = ");
switch(date2.mm)
{
case 1 : printf("Jan"); break;
case 2 : printf("Feb"); break;
case 3 : printf("Mar"); break;
case 4 : printf("Apr"); break;
case 5 : printf("may"); break;
case 6 : printf("Jun"); break;
case 7 : printf("July"); break;
case 8 : printf("Aug"); break;
case 9 : printf("spt"); break;
case 10 : printf("oct"); break;
case 11 : printf("nov"); break;
case 12 : printf("Dec"); break;
default : printf("Invalid no");
       }
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.
ANS)

#include<stdio.h>
#include<conio.h>
struct hotel
{
char name[20],address[30];
int  rating,charge,noofroom;
}hotel[3];

void main()
{

int i=0,budget;
clrscr();
for(i=0;i<3;i++)
{
printf("\nEnter hotel name = ");  flushall();
gets(hotel[i].name);
printf("\nEnter adress of hotel = ");
gets(hotel[i].address);
printf("\nEnter rating = ");
scanf("%d",&hotel[i].rating);
printf("Enter no of rooms = ");
scanf("%d",&hotel[i].noofroom);
printf("\nEnter charges = ");    flushall();
scanf("%d",&hotel[i].charge);
}
clrscr();
printf("\nEnter your budget = ");
scanf("%d",&budget);
for(i=0;i<3;i++)
{
if(hotel[i].charge<=budget)
{
printf("\n\nHotel = %d \n-------------------------",i+1);

printf("\nHotel name = %s",hotel[i].name);

printf("\nHotel Address = %s",hotel[i].address);

printf("\nHotel rating = %d",hotel[i].rating);

printf("\nEnter charges = %d",hotel[i].charge);
}
}
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.
ANS)
#include<stdio.h>
#include<conio.h>
struct col
{
    char st[20];
    int enc,mec,mac,un,total;
};
void main()
{
    struct col x[40];
    int i,n,j=0,k=1;
    clrscr();
    printf("How many state are you enter? = ");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
printf("Enter %d state = ",i+1);
scanf("%s",x[i].st);
printf("Enter no of engineering colleges = ");
scanf("%d",&x[i].enc);
printf("Enter no of medical colleges = ");
scanf("%d",&x[i].mec);
printf("Enter no of management colleges = ");
scanf("%d",&x[i].mec);
x[i].total=x[i].enc+x[i].mec+x[i].mac;
    }
    printf("\n \n");
    printf("\n State\t\t Eng.colleges\t Medi.colleges\t Man.colleges \t Total colleges");
    for(i=0;i<n;i++)
    {
printf("\n %s\t\t %d\t\t %d\t\t %d \t\t %d",x[i].st,x[i].enc,x[i].mec,x[i].mec,x[i].total);
    }
    for(i=0;i<n;i++)
    {
if(x[i].total>=k)
{
    k=x[i].total;
    j++;
}
    }
    j--;
     printf("\n \n");
     printf("This state having highest colleges = ");
    printf("\n State\t\tTotal colleges");

    printf("\n %s\t\t%d",x[j].st,k);
    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.
ANS)
#include<stdio.h>
#include<conio.h>
struct tm
{
    int hh,mm,ss;
};
void sub(struct tm,struct tm,struct tm*);
void dispay(struct tm);
void main()
{
    struct tm t1,t2,ans;
    clrscr();
    printf("\n enter first time(hh/mm/ss) = \t");
    scanf("%d%d%d",&t1.hh,&t1.mm,&t1.ss);
    printf("\nenter second  time(hh/mm/ss) = \t");
    scanf("%d%d%d",&t2.hh,&t2.mm,&t2.ss);
    if(t1.hh<t2.hh)
    {
printf("\n invalid time");
printf("\n please enter first time > second time");
    }
    else{
    sub(t1,t2,&ans);

    printf("\nFirst time = ");
    dispay(t1);
    printf("\nSecond time =");
    dispay(t2);
    printf("\n answer = ");
    dispay(ans);
    }
    getch();
}
void dispay(struct tm tmp)
{
    printf("\n %d:%d:%d",tmp.hh,tmp.mm,tmp.ss);
}
void sub(struct tm t1,struct tm t2,struct tm *p )
{


if(t1.ss>=t2.ss)
{
p->ss=t1.ss-t2.ss;
}
else
{
    t1.mm--;
    t1.ss=t1.ss+60;
    p->ss=t1.ss-t2.ss;
}
if(t1.mm>=t2.mm)
{
p->mm=t1.mm-t2.mm;
}
else
{
    t1.hh--;
    t1.mm=t1.mm+60;
    p->mm=t1.mm-t2.mm;
}
p->hh=t1.hh-t2.hh;

}



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.
ANS)
#include<stdio.h>
#include<conio.h>
struct state
{
    char st[20];
    int pop,lirt,cpic;
};
void main()
{
    struct state x[40];
    int i,n,j=0,l=0,k=0,m=0;
    clrscr();
    printf("how many state U wnt to enter? = ");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
printf("\nenter %d  state name = ",i+1);
scanf("%s",x[i].st);
printf("\nenter population of state = ");
scanf("%d",&x[i].pop);
printf("\nenter literacy rate of state = ");
scanf("%d",&x[i].lirt);
printf("enter capita income of state = ");
scanf("%d",&x[i].cpic);
    }
    clrscr();
    printf("\n \n");
    printf("\n state name\t population \t literacy rate(per.) \t capita income\n");
    for(i=0;i<n;i++)
    {
printf("\n %s\t\t %d\t\t %d\t\t\t %d ",x[i].st,x[i].pop,x[i].lirt,x[i].cpic);
    }
    for(i=0;i<n;i++)
    {
if(x[i].lirt>=k)
{
    k=x[i].lirt;
    j++;
}
    }
    j--;
     printf("\n \n");
     printf("This state having highest literacy rate \n");
    printf("\n state\t\t literacy rate");

    printf("\n %s\t\t  %d",x[j].st,k);
    for(i=0;i<n;i++)
    {
if(x[i].cpic>=l)
{
    l=x[i].cpic;
    m++;
}
    }
    m--;
printf("\n \n");
printf("\nThis state having highest capital income");
printf("\n state\t\t capita income");

printf("\n %s\t\t %d",x[m].st,l);
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.
ANS)

#include<stdio.h>
#include<conio.h>
void main()
{
struct emp
{
char name[20];
int basic,da,hr,net;
}emp[3];

int i,j;
char temp[10];
clrscr();
for(i=0;i<3;i++)
{

printf("\nEnter Employee name = ");  flushall();
gets(emp[i].name);

printf("\nEnter Basic Pay = ");
scanf("%d",&emp[i].basic);

}
clrscr();


for(i=0;i<3;i++)
{
for(j=i+1;j<3;j++)
{
if(emp[i].basic>emp[j].basic)
{
      emp[i].basic=emp[i].basic+emp[j].basic;
      emp[j].basic=emp[i].basic-emp[j].basic;
      emp[i].basic=emp[i].basic-emp[j].basic;
      strcpy(temp,emp[j].name);
      strcpy(emp[j].name,emp[i].name);
      strcpy(emp[i].name,temp);
}
}
}
for(i=0;i<3;i++)
{
emp[i].da=(emp[i].basic*50)/100;
emp[i].hr=(emp[i].basic*20)/100;
emp[i].net=emp[i].basic+emp[i].hr+emp[i].da;
}
for(i=0;i<3;i++)
{
printf("\n Employee %d....",i+1);
printf("\n Name = %s \n basic pay = %d",emp[i].name,emp[i].basic);
printf("\n Dear allowance = %d \tHouse rent = %d",emp[i].da,emp[i].hr);
printf("\n Net salary = %d \n--------------------------\n",emp[i].net);
}
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. 
ANS)

#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()
{
clrscr();
printf("\nEnter name of state = ");
gets(pop.s1.name);
printf("Enter Men pop = ");
scanf("%ld",&pop.men);
printf("Enter Women pop = ");
scanf("%ld",&pop.women);
clrscr();

pop.s1.total=pop.men+pop.women;

printf("\nState Name = %s",pop.s1.name);
printf("\nMen Population = %ld",pop.men);
printf("\nWomen Population = %ld",pop.women);
printf("\nTotal Population in %s = %ld",pop.s1.name,pop.s1.total);
getch();
}

Comments

Popular posts from this blog

C++ Practice Program

Cloud Computing Important Question-Answer for University Exam

Software Testing Gujarat University Important Questions For Exam