Webocreation

Monday, June 15, 2009

example programs in C(read before copying)

write the header file by yourself

AP in C to display the information of a person
#include
#include
#include void main()
{
int ward_num=6; // 2 bytes 32767 to -32768
char sex='f'; // 1 byte single character 'f' or 'm'
char name[30]="Laxmi Prasad Devkota"; // string or array of string
float phone=4781698; // 4 bytes containing decimal/exponent
double num_of_cell_in_body=9999999999999999999999999999; //8 bytes
// the basic data types are int,char,float,double
// besides, the data type qualifers are short, long, signed
// and unsigned
// for example short int, long int, unsigned in
clrscr();
printf("\nname:=%s:",name);
printf("\nphone=%.0f",phone);
printf("\nsex=%c",sex);
printf("\nbody cell=%f",num_of_cell_in_body);
printf("\nWard=%d",ward_num);
getch();
}



#include
#include
void main()
{
int first,second,lcm;
/*finding L.C.M.*/
clrscr();
printf("Enter first number:");
scanf("%d",&first);
printf("Enter second number:");
scanf("%d",&second);
lcm=first;
do
{
if(lcm%first==0)
if(lcm%second==0)
break;
lcm++;
}while(1);
printf("\n\nL.C.M.=%d",lcm);
getch();
}

#include
void main()
{
int age[5],temp,pass, i;
printf(“Enter 5 elements:\n”);
for(i=0;i<5;i++) pass="0;pass<4;pass++)" i="0;i<=">age[i+1])
{
temp=age[i];
age[i]=age[i+1];
age[i+1]=temp;
}
}
}
printf(“Sorted list:\n”);
for(i=0;i<5;i++)>
#include
void main()
{
int a,b,choice;
clrscr();
printf("Enter first numbe:");
scanf("%d",&a);
printf("Enter second number:");
scanf("%d",&b);
printf("\n\n\n\nChoose the number\n");
printf("\n1 for add\n2 for subtract\n3 for multiple\n4 for divide\n");
scanf("%d",&choice);
switch (choice)
{
case 1:
printf("Sum=%d",a+b);
break;
case 2:
printf("Difference=%d",a-b);
break;
case 3:
printf("Product=%d",a*b);
break;
case 4:
printf("Quotient=%d",a/b);
break;
};
printf("\n Thank you");
getch();
}






#include
#include
void main()
{
int a,b,choice;
clrscr();
printf("Enter first numbe:");
scanf("%d",&a);
printf("Enter second number:");
scanf("%d",&b);
printf("\n\n\n\nChoose the number\n");
printf("\n1 for add\n2 for subtract\n3 for multiple\n4 for divide\n");
scanf("%d",&choice);
switch (choice)
{
case 1:
printf("Sum=%d",a+b);
break;
case 2:
printf("Difference=%d",a-b);
break;
case 3:
printf("Product=%d",a*b);
break;
case 4:
printf("Quotient=%d",a/b);
break;
};
printf("\n Thank you");
getch();
}
#include
#include
#include
#include
void f1(int a);
int p=0;
void main()
{
int num;
//exit(1);
clrscr();
while(1)
{
printf("Enter only even number");
scanf("%d",&num);
if(num%2==0)
f1(num);
} }

void f1(int x )
{
int static y;
printf("\nthank u you number is %d",x);
y++;
if(y>5)
{
printf("\ntired!");
getch();
exit(1);
}
//clrscr();
}
1. Initialize or store roll, name and two subjects' marks of a student in a structure and display it.
#include
#include
void main()
{
struct mydatatype{
int roll;
char sname[20];
int comp,eng;
};
struct mydatatype student = { 1,"Ram",24,23 };
clrscr();
printf("\nYour information\n\n");
printf("\nRoll %d",student.roll);
printf("\nName %s",student.sname);
printf("\nComputer %d",student.comp);
printf("\nEnglish %d",student.eng);
getch();
}
2. Initialize or store roll, name and two subjects' marks of 3 students in a structure and display it.
#include
#include
void main()
{
int i;
struct mydatatype{
int roll;
char sname[20];
int comp,eng;
};
struct mydatatype student[] = { 1,"Ram",24,23,
2, "Seeta",21,12,
3, "Laxman", 12, 11,
};
clrscr();
printf("\nYour information\n\n");
for(i=0;i<=2;i++) { printf("\nRoll %d",student[i].roll); printf("\nName %s",student[i].sname); printf("\nComputer %d",student[i].comp); printf("\nEnglish %d",student[i].eng); } getch();} 3. Get roll, name and two subjects marks of n students in a structure and display it. #include
#include
void main()
{
int i,n;
struct mydatatype{
int roll;
char sname[20];
int comp,eng;
};
struct mydatatype student[80];
clrscr();
printf("\nHou many students?");
scanf("%d",&n);
for(i=0;i$n;i++) //type < i="0;i$n;i++)//type" i="0;i<4;i++)" roll="i+1;" i="i-1;">
main()
{
int i;
struct exam
{
int prg;
int math;
};
struct exam subjcect;

struct info
{
char sname[20];
int phone;
};
struct info names;

struct details
{
int roll;
struct exam subject;
struct info names;
};
struct details students[4];
clrscr();
for(i=0;i<4;i++) roll="i+1;" i="i-1;">
#include
typedef struct
{
int h,m,s;
}TIME;
TIME calculater(TIME start, TIME stop,int check);
void main()
{
TIME start,stop,sum,diff;
int hh,mm,ss;
clrscr();
printf("Enter start time:\nHour:");
scanf("%d",&hh);
printf("\nMinutes:");
scanf("%d",&mm);
printf("\nSeconds:");
scanf("%d",&ss);
start.h=hh;
start.m=mm;
start.s=ss;
printf("Enter stop time:\nHour:");
scanf("%d",&hh) ;
printf("\nMinutes:");
scanf("%d",&mm);
printf("\nSeconds:");
scanf("%d",&ss);
stop.h=hh;
stop.m=mm;
stop.s=ss;
sum=calculater(start,stop,1);
printf("\nThe sum is\n");
printf("\n%dhh%dmm%dss",sum.h,sum.m,sum.s);
diff=calculater(start,stop,0);
printf("\nThe diff is\n");
printf("\n%dhh%dmm%dss",diff.h,diff.m,diff.s);
getch();
}
TIME calculater(TIME a, TIME b,int check)
{
TIME sum;
int x;
int temp;
if (check==1)
temp=a.h*3600+a.m*60+a.s+b.h*3600+b.m*60+b.s ;// meaning less to add
else
temp=((b.h*3600+b.m*60+b.s)-(a.h*3600+a.m*60+a.s)) ;

sum.s=temp%60;
temp/=60;
sum.m=temp%60;
sum.h=temp/60;
return sum;
}















































// Get roll, name and two subjects marks of n students in a structure and display it.
#include
#include
struct age
{
float y,m,d;
}dob,now,dif;
void main()
{
age cal(age birth, age now);
clrscr();
printf("Enter year,month and date of date of birth:");
scanf("%f",&dob.y);
scanf("%f",&dob.m);
scanf("%f",&dob.d);

printf("Enter year,month,and date of system date");
scanf("%f",&now.y);
scanf("%f",&now.m);
scanf("%f",&now.d);
dif=cal(dob,now);
printf("\nRequired Age:\n:%.0f:",dif.y);
printf(" year %.0f",dif.m);
printf(" month %.0f day",dif.d);
getch();
}
age cal(age x, age y)
{
float temp1,temp2;
temp1=(y.y*365+y.m*60+y.d)-(x.y*365+x.m*60+x.d);
temp2=temp1;
while (temp2>=30)
{ temp2-=30;};
dif.d=temp2;
temp2=temp1/30;
while(temp2>=12)
{temp2-=12;};
dif.m=temp2;
dif.y=temp1/360;
return (dif);
}









/* reorder a one dimensional, integer array from samllest to largest
using pointer notation*/
#include
#include
#include
void reorder(int n, int *x);
void main()
{
int i,n,*x;
clrscr();
printf("How many numbers wil be entered?");
scanf("%d",&n);
x=(int*) malloc(n*sizeof(int));
for(i=0;i$n;++i)//type < i="0;i$n;i++)//type" item="0;item$n-1;++item)//type" i="item+1;i$n;++i)//type" temp="*(x+item);">
#include
#include
void main()
{
FILE *ptr;
int num,i;
int identity,off_name,occu;
char name[10];
clrscr();
printf("How many reocrds?");
scanf("%d",&num);
ptr=fopen("employee.dat","w");
for(i=0;i$num;i++)//type <>
#include
#include
void main()
{
FILE *ptr;
char c;
clrscr();
ptr=fopen("myfile.dat","w");
do
putc(toupper(c=getchar()),ptr);
while(c!='\n');
fclose(ptr);
}



































2} Write a program to read one line text from a data file.
#include
#include
#include
#define NULL 0
void main()
{
FILE *ptr;
char c;
clrscr();
if((ptr=fopen("myfile.dat","r"))==NULL)
printf("File not found.");
else
do
putchar(c=getc(ptr));
while(c!='\n');
fclose(ptr);
getch();
}
































3} Write a program to store n names in a data file.
#include
#include
#include
void main()
{
FILE *ptr;
int num,i;
char name[10];
clrscr();
printf("How many reocrds?");
scanf("%d",&num);
ptr=fopen("myfile.dat","w");
for(i=0;i$num;i++)//type <>
#include
#include
#include
#define NULL 0
# define spac " "
void main()
{
FILE *ptr;
char c;
int i=0,j;
clrscr();
if((ptr=fopen("myfile.dat","r"))==NULL)
printf("File not found.");
else
do
{
c=getc(ptr);
if(c==',')
putchar('\n');
else
putchar(c);
if(c==' ')
j++;
i++;
}while(feof(ptr)==0);
fclose(ptr);
getch();






















5} }Write a program to append some names in an existing data file.
#include
#include
#include
#include
void main()
{
FILE *fptr;
char name[10],c;
clrscr();
fptr=fopen("myfile.txt","a");
do
{
printf("Name:");
scanf("%s",name);
fprintf(fptr,name);
fprintf(fptr,",");
printf("Press \"y\" to continue\n");
c=getche();
}while(toupper(c)=='\Y');
fclose(fptr);
}





























6} Write a program to create/append/disp data of a data file. Also use structure.
#include
#include
#include
# define null 0
struct info{
int roll;
char name[20];
char of_name[20];
char oc_name[20];

};
FILE *ptr;
void dispmenu();
void create(void);
void add(void);
info dispdata(void);
void main()
{
int choice;
info student;
repeat:
dispmenu();
scanf("%d",&choice);
if(choice<1>4)
goto repeat;
else if (choice==1)
create();
else if (choice==2)
add();
else if (choice==3)
{
clrscr();
if((ptr=fopen("myfile.dat","r"))==null)
printf("File not found.");
else
{
choice=0;
do
{
student=dispdata();
choice++;
printf("%d.%d",choice,student.roll);
printf("%s\n",student.name);
}while(feof(ptr)==0);
getch();
fclose(ptr);
}
}
else if(choice==4)
exit(1);
goto repeat;
}
void dispmenu()
{
clrscr();
printf("\n1.Create New File.\n2.Add(append)");
printf("\n3.Display\n4.Exit\nChoose 1 to 4");
}
void create()
{
info student;
ptr=fopen("myfile.dat","w");
clrscr();
printf("Roll:");
scanf("%d",&student.roll);
printf("name");
scanf("%s",student.name);
?????????????????
fprintf(ptr,"%d%s\n",student.roll,student.name);
fclose(ptr);
}
void add()
{
info student;
clrscr();
ptr=fopen("myfile.dat","a");
printf("Roll:");
scanf("%d",&student.roll);
printf("name:");
scanf("%s",student.name);
fprintf(ptr,"%d%s\n",student.roll,student.name);
fclose(ptr);
}
info dispdata()
{
info student;
{
fscanf(ptr,"%d",&student.roll);
fscanf(ptr,"%[^\n]",student.name);
return(student);
}
}









7} Get a phrase and print in alphabetical order.
#include
#include
#include
#define eol '\n'
void main()
{
char text[80],abc;
int i,j,k;
clrscr();
printf("\nEnter the text to display in alphabetical order\n");
for(i=0;(text[i]=getchar())!=eol;++i)
;
k=i-1;
printf("\n\n\n");
for(i=0;i$k;i++)//type < j="0;j$k;j++)//type" i="j;i" abc="text[j];">=0;--i)
putchar(text[i]);
getch();

}
Examples
#include
main()
{
FILE *buffer;
char text[20];
clrscr();
printf("Enter you name to store in data file: ");
scanf("%s",text);
buffer=fopen("txt.dat","w");
fprintf(buffer,text);
fclose(buffer);
/*see the text from dos */
}
/*---------------------------------------------------------------------------*/#include
struct record
{
char name[20];
char address[20];
} student[5];

main()
{
FILE *buffer;
char fname[20];
int num,i;
clrscr();
printf("Enter the file name: ");
scanf("%s",fname);
buffer=fopen(fname,"a");
printf("How many students? ");
scanf("%d",&num);
for(i=1;i<=num;i++) { printf("Name: "); scanf("%s",student[i].name); printf("Address: "); scanf("%s",student[i].address); fprintf(buffer,student[i].name); fprintf(buffer,student[i].address); fprintf(buffer,"\n"); } fclose(buffer); } #include
#define finished 0
main()
{
FILE *buffer;
char filename[10];
char text[20];
clrscr();
printf("Enter existing data file name to read: ");
scanf("%s",filename);
if((buffer=fopen(filename,"r"))==finished)
printf("Wrong file name! ");
else
{
fscanf(buffer,"%s",text);
printf("%s",text);
}
getch();
fclose(buffer);
}































/*********PROJECT WORK**************/
/*Write a program to create/append/disp data of a data file.
Also use structure.*/
#include
#include
#include
#include
# define null 0
struct info{
int roll;
char name[20];
}student;
FILE *ptr,*ptr1;
void dispmenu();
void create(void);
void add(void);
void dispdata(void);
void disp_one(void);
void modify(void);
void del_one(void);
void del_all(void);
void main()
{
int choice;
repeat:
dispmenu();
scanf("%d",&choice);
if(choice<1>8)
goto repeat;
else if(choice==1)
create();
else if(choice==2)
add();
else if(choice==3)
dispdata();
else if(choice==4)
disp_one();
else if(choice==5)
modify();
else if(choice==6)
del_one();
else if(choice==7)
del_all();
else if(choice==8)
exit(1);
goto repeat;
}
void create()
{
ptr=fopen("myfile.dat","w");
clrscr();
printf("Roll:");
scanf("%d",&student.roll);
printf("name");
scanf("%s",student.name);
fprintf(ptr,"%d%s\n",student.roll,student.name);
fclose(ptr);
}
void add()
{
clrscr();
ptr=fopen("myfile.dat","a");
printf("Roll:");
scanf("%d",&student.roll);
printf("name:");
scanf("%s",student.name);
fprintf(ptr,"%d%s\n",student.roll,student.name);
fclose(ptr);
}
void dispdata()
{
clrscr();
if((ptr=fopen("myfile.dat","r"))==null)
printf("File not found.");
else
{
while(1)
{
fscanf(ptr,"%d",&student.roll);
fscanf(ptr,"%[^\n]",student.name);
if(student.roll==null)
break;
printf("%d ",student.roll);
printf("%s\n",student.name);
student.roll=null;
}
getch();
fclose(ptr);
}
}
void disp_one()
{
int roll;
clrscr();
if((ptr=fopen("myfile.dat","r"))==null)
printf("File not found.");
else
{
printf("Enter roll:");
scanf("%d",&roll);
do
{
fscanf(ptr,"%d",&student.roll);
fscanf(ptr,"%[^\n]",student.name);
if(roll==student.roll)
{
printf("%d ",student.roll);
printf("%s\n",student.name);
break;
}
}while(feof(ptr)==0);
getch();
fclose(ptr);
}
}
void modify()
{
int roll;char name[20];
clrscr();
if((ptr=fopen("myfile.dat","r"))==null)
printf("File not found.");
else
{
printf("Enter roll:");
scanf("%d",&roll);
printf("Enter name:");
scanf("%s",name);
ptr1=fopen("test","w");
do
{
fscanf(ptr,"%d",&student.roll);
fscanf(ptr,"%[^\n]",student.name);
if(roll==student.roll)
{
student.roll=roll;
strcpy(student.name,name);
}
fprintf(ptr1,"%d%s\n",student.roll,student.name);
}while(feof(ptr)==0);
getch();
fclose(ptr);
fclose(ptr1);
remove("myfile.dat");
rename("test","myfile.dat");
}
}
void del_one()
{
int roll;char name[20];
clrscr();
if((ptr=fopen("myfile.dat","r"))==null)
printf("File not found.");
else
{
printf("Enter roll:");
scanf("%d",&roll);
ptr1=fopen("test","w");
while(1)
{
fscanf(ptr,"%d",&student.roll);
fscanf(ptr,"%[^\n]",student.name);
if(student.roll==null)
break;
else if(student.roll==roll)
;
else
{
fprintf(ptr1,"%d%s\n",student.roll,student.name);
student.roll=0;
}
}
fclose(ptr);
fclose(ptr1);
remove("myfile.dat");
rename("test","myfile.dat");
}
}

void del_all()
{
remove("myfile.dat");
}
void dispmenu()
{
clrscr();
printf("\n1.Create New File.\n2.Add more(append)");
printf("\n3.Display all\n4.Disp particular");
printf("\n5.Modify one\n6.Delete one");
printf("\n7.Delete all\n8.Exit\nChoose 1 to 8 only");
}

No comments:

Post a Comment