Archive for the ‘Lab 1’ Category

C program-Structure of library

Posted: January 19, 2012 in Lab 1

Write a Menu Driven program that depicts the working of a library. The menu options should be:

i) Add book information
ii) Display book information
iii) List all books of given author
iv) List the title of specified book
v) List the count of books in the library
vi) List the books in the order of accession number.
vii) Exit


#include<stdio.h>
#include<stdlib.h>
int main()
{
	struct lib
	{
		int accession,flag;
		float price;
		char name[20],authname[20];
	};
	struct lib l[20];
	int ch,i=0,acc,j=0,x;
	char author[20];
	while(x=1)
	{
	printf("What do u want to do?\n");
	printf("1.Add book information\n");
	printf("2.Display book information\n");
	printf("3.List all books of given author\n");
	printf("4.List the title of specified book\n");
	printf("5.List the count of books in the library\n");
	printf("6.List the books in the order of accession no\n");
	printf("7.Exit\n");
	scanf("%d",&ch);
		switch(ch)
	{
		case 1:
		{
				printf("Enter the name of the book\n");
			//scanf("%s",l[j].name);
			fflush(stdin);
			gets(l[j].name);

			 printf("Enter the author name\n");
			//scanf("%s",l[j].authname);
         fflush(stdin);
			gets(l[j].authname);
			printf("Enter price\n");
			scanf("%f",&l[j].price);
			printf("press 0 if book is issued and 1if available\n");
			scanf("%d",&l[j].flag);
			printf("Record added successfully\n");
			l[j].accession=j;
			j++;
			break;
		}
			case 2:
		{
           for(i=0;i<j;i++)
				puts(l[i].name);
			printf(" ");
			puts(l[i].authname);
			printf("%f ",l[i].price);
			if(l[i].flag==0)
				printf("Book is available\n");
			else
				printf("Book is not available\n");
			break;
		}
			case 3:
		{
			printf("Enter the name of author\n");
			gets(author);
			for(i=0;i<j;i++)
			{
				if(l[i].authname==author)
				{
					puts(l[i].name);
						printf("\n");
				}
			}
			break;
		}
			case 4:
		{
			printf("Enter the accession no. of the book\n");
			scanf("%d",&acc);
			for(i=0;i<j;i++)
				if(l[i].accession==acc)
				{
					puts(l[i].name);
					printf("\n");
				}
			break;
		}
			case 5:
		{
			printf("%d",j);
		break;
		}
			case 6:
		{
			for(i=0;i<j;i++)
				{
				puts(l[i].name);
				printf("\n");
			}
		break;
		}
			case 7:
			{
				x=2;
			break;
			}
	}
return 0;
}
}






		

C program-Structure of cricket

Posted: January 19, 2012 in Lab 1

Define a structure called cricket that will describe the following
information:

player name
team name
batting average

using cricket ,declare an array player with 50 elements and write a program to read the information about all the players and print a team wise list contaning names of players with their batting average.


#include<stdio.h>
struct cricket
{
char name[20];
char team[20];
float avg;
}player[20];
void main()
{
int n,i;
printf("Enter the number of players");
scanf("%d",&n);
for(i=0;i<n;i++)
 {
printf("Enter player name : ");
fflush(stdin);
gets(player[i].name);
printf("Enter team name : ");
gets(player[i].team);
printf("Enter batting average : ");
scanf("%f",&player[i].avg);
}
printf("PLAYER NAME\tTEAM NAME\tBATTING AVERAGE\n");
for(i=0;i<n;i++)
{
printf("%s\t\t%s\t\t%f\n",player[i].name,player[i].team,player[i].avg);
 }

}

Employee structure program

Posted: January 16, 2012 in Lab 1

There is a structure called employee that holds information like employee code, name date of joining. Write a program to create an array of structures and enter some data into it. Then ask the user to enter current date. Display the names of those employees whose tenure is 3 or more than 3 years according to the given current date.

#include<stdio.h>
int main()
{
	struct employee
	{
		char name[40];
		int code,doj,moj,yoj;

	};
	struct employee e[3];
	int i,d,m,y,yr;
      for(i=0;i<3;i++)
	{
		printf("Enter employee name & code\n");
		gets(e[i].name);
		scanf("%d",&e[i].code);
		printf("Enter date of joining dd/mm/yy\n");
		scanf("%d/%d/%d",&e[i].doj,&e[i].moj,&e[i].yoj);
		printf("Enter current date dd/mm/yy\n");
		scanf("%d/%d/%d",&d,&m,&y);
		//d=15; m=1; y=12;
		yr=y-e[i].yoj;
		if(yr>3)
		{
			printf("%s",e[i].name);
			continue;
		}
		if(yr==3)
			if(e[i].moj>m)
			{
				printf("%s",e[i].name);
				continue;
			}
		if(e[i].moj==m)
			if(e[i].doj>=d)
				printf("%s",e[i].name);
		}
}

Automobile company structure program.

Posted: January 16, 2012 in Lab 1

An automobile company has serial number for engine parts starting from AA0 to FF9.The other characteristics of parts to be specified in a structure are : Year of manufacture, material and quantity manufactured.

a. Specify a structure to store information corresponding to a part.
b. Write a program to retrive information on parts with serial
numbers between BB1 and CC6.

#include<stdio.h>
#include<stdlib.h>
int main()
{
	struct parts
	{
		char sl[4],material[10];
		int yr,qty;
	};
	int i;
	struct parts p[3];
	for(i=0;i<3;i++)
	{
		printf("Enter sl no\n");
                fflush(stdin);
		gets(p[i].sl);
		printf("Enter yr of manufactur , material and qtty\n");
		scanf("%d%s%d",&p[i].yr,&p[i].material,&p[i].qty);
	}
	for(i=0;i<3;i++)
	{
		if(p[i].sl[0]=='A')
			continue;
		if(p[i].sl[0]>=100&&p[i].sl[0]<=102)
			continue;
		if((p[i].sl[0]=='B'&&p[i].sl[2]=='1')||(p[i].sl[0]=='C'&&p[i].sl[0]>'6'))
			continue;
		printf("%s %d %s %d\n",p[i].sl,p[i].yr,p[i].material,p[i].qty);
	}
	return 0;
}
#include<stdio.h>
#include<math.h>
int main()
{
long long b,n,i=0,ans=0,*ptr;
printf("enter base??\n");
scanf("%lld",&b);
printf("Enter no.\n");
scanf("%lld",&n);
ptr=&n;
while(*ptr>0)
{
ans=ans+(*ptr%10)*pow(b,i);
*ptr=*ptr/10;
i++;
}
printf("%lld\n",ans);
return 0;
}
#include<stdio.h>
#include<string.h>
int main()
{
char str[100],*ptr;
int i,l,count=0;
printf("Enter any string\n");
gets(str);
l=strlen(str);
ptr=str;
for(i=1;i<l;i++)
{
if(*(ptr+i-1)!=' '&&*(ptr+i)==' '&&*(ptr+i+1)!=' ')
count++;
}
printf("%d\n",count);
}
#include<stdio.h>
#include<string.h>
int main()
{
char str[100],*ptr,i,l;
printf("Enter any string\n");
gets(str);
l=strlen(str);
ptr=str;
printf("%c",*(ptr+0));
for(i=1;i<l;i++)
{
if(*(ptr+i-1)==' ')
printf(" %c ",*(ptr+i));
}
}
#include<stdio.h>
int main()
{
char str1[100],*ptr;
printf("Enter any string in upper case\n");
gets(str1);
ptr=str1;
for(;*ptr!='\0';ptr++)
{

if(*ptr==' ')
continue;
if(*ptr>=65&&*ptr<=90)
*ptr=*ptr+32;
}
puts(str1);
}