Friday, May 30, 2014

PROGRAM ON ENUM

#include<stdio.h>
#include<conio.h>
void main()
{
enum mon
{
jan=1,feb,mar,apr=1,may,jun
}m1,m2;
enum month
{
apr,
jul=1   //invalid- multiple declaration for apr
}m3;
clrscr();
printf("m1=%d",m1);      // m1=garbage value
m1=jan;             //assign m1 an enumerator
printf("\nm1=%d",m1);    //m1=1
m1=1;               //assign m1 an integer constant since m1 can hold integer value
printf("\nm1=%d",m1);    //m1=1
m1=8;
printf("\nm1=%d",m1);  //m1=8
printf("\n enter the value of m\n");
scanf("%d",&m1);
printf("\nm1=%d",m1);  //m1=value given by user
//feb=5; lvalue required
//u can directly print value of an enumerator
printf("\n%d",may); //2
m2=aug; //invalid
getch();
}

FILE HANDLING- USE OF FPRINTF & FSCANF

#include<conio.h>
#include<stdio.h>
void main()
{
  FILE *fp1;
  typedef struct student
  {
  char name[20];
  int age;
  }stu;
  stu s1;
  int i,n;
  clrscr();
  fp1=fopen("stu.txt","w");
  if(fp1==NULL)
  {
printf("file creation error\n");
getch();
exit(1);
  }
  printf("enter the no of students\n");
  scanf("%d",&n);
  for(i=0;i<n;i++)
  {
printf("enter the name and age\n");
scanf("%s%d",s1.name,&s1.age);
fprintf(fp1,"%s %d\n",s1.name,s1.age);
  }
  fclose(fp1);

  fp1=fopen("stu.txt","r");
  if(fp1==NULL)
  {
printf("file does not exist\n");
getch();
exit(1);
  }
  else
  {
  while((fscanf(fp1,"%s%d",s1.name,&s1.age))!=EOF)
{
printf("name=%s age=%d\n",s1.name,s1.age);
}
  }
  fclose(fp1);
  getch();
}

FILE HANDLING USE OF FGETS & FPUTS

#include<conio.h>
#include<stdio.h>
void main()
{
  char str[30];
  FILE *fp1;
  clrscr();
  fp1=fopen("test2.txt","w");
  if(fp1==NULL)
  {
printf("file creation error\n");
getch();
exit();
  }
  else
  {
printf("enter the text\n");
while(gets(str)!=NULL)
{
  fputs(str,fp1);
  fputs("\n",fp1);
}
  }
  fclose(fp1);

  fp1=fopen("test2.txt","r");
  if(fp1==NULL)
     {
printf("file does not exist\n");
getch();
exit();
     }
  else
     {
while(fgets(str,30,fp1)!=NULL)
{
  puts(str);
}
     }
  fclose(fp1);
  getch();
}

FILE HANDLING USE OF FPUTW & FGETW

 #include<conio.h>
 #include<stdio.h>
 void main()
 {
  FILE *fp1;
  int i,n,val;
  clrscr();
  fp1=fopen("num1.dat","wb");
  if(fp1==NULL)
  {
     printf("file creation error\n");
     exit();
  }
 else
 {
     printf("enter the no of values\n");
     scanf("%d",&n);
     printf("enter the values\n");
     for(i=1;i<=n;i++)
     {
scanf("%d",&val);
putw(val,fp1);
     }
 }
 fclose(fp1);

 fp1=fopen("num1.dat","rb");
 if(fp1==NULL)
   {
printf("file does not exist\n");
getch();
exit();
   }
 else
   {
val=getw(fp1);
while(!feof(fp1))  //this will check for real end of file
//while(val!=EOF)  //this will stop reading as it get val=-1 even before real EOF
{
   printf("\n%d",val);
   val=getw(fp1);
}
   }
 fclose(fp1);
 getch();
 }

FILE HANDLING USE OF FPUTC & FGETC

#include<stdio.h>
#include<conio.h>
void main()
{
  FILE *fp1;
  int ch;
  clrscr();
  fp1=fopen("test1.txt","w");
  if(fp1==NULL)
  {
     printf("file creation error\n");
     getch();
     exit();
  }
  else
  {
     printf("enter the character\n");
     ch=getchar();
     while(ch!=EOF)
     {
     fputc(ch,fp1);
     ch=getchar();
     }
  }
  fclose(fp1);

  fp1=fopen("test1.txt","r");

  if(fp1==NULL)
  {
     printf("file does not exist\n");
     getch();
     exit();
  }
  else
  {
     ch=fgetc(fp1);
     while(ch!=EOF)
     {
printf("%c",ch);
ch=fgetc(fp1);
     }
  }
  fclose(fp1);
  getch();
}

WRITE A PROGRAMME TO COMPARE THE MEMORY ALLOCATIONS BETWEEN STRUCTURE AND UNION VARIABLE

#include<stdio.h>
#include<conio.h>
void main()
{
typedef union
{
int roll;
char grade;
float per;
}uni1;

typedef struct
{
int roll;
char grade;
float per;
}str1;

uni1 u1;
str1 s1;
clrscr();
printf("\nSTRUCTURE");
printf("\nsize of s1 is %d",sizeof(s1));
printf("\naddress of s1 is %u",&s1);
printf("\naddress of s1.roll is %u",&s1.roll);
printf("\naddress of s1.grade is %u",&s1.grade);
printf("\naddress of s1.per is %u",&s1.per);

printf("\nUNION");
printf("\nsize of u1 is %d",sizeof(u1));
printf("\naddress of u1 is %u",&u1);
printf("\naddress of u1.roll is %u",&u1.roll);
printf("\naddress of u1.grade is %u",&u1.grade);
printf("\naddress of u1.per is %u",&u1.per);
getch();
}

WRITE A PROGRAMME TO READ AND PRINT THE FOLLOWING INFORMATION USING UNION (NAME, ROLLNO, PERCENTAGE, GRADE)

#include<conio.h>
#include<stdio.h>
void main()
{
union student
{
char name[20];
int roll;
float per;
char grade;
}u1;
clrscr();

printf("enter the name\n");
scanf("%s",u1.name);
printf("name->%s\n",u1.name);
printf("enter the roll\n");
scanf("%d",&u1.roll);
printf("roll no->%d\n",u1.roll);
printf("enter the percentage\n");
scanf("%f",&u1.per);
printf("percentage->%f\n",u1.per);
printf("enter the grade\n");
fflush(stdin);
scanf("%c",&u1.grade);
printf("grade->%c",u1.grade);

printf("\nname->%s\n",u1.name);
printf("\nroll no->%d\n",u1.roll);
printf("\npercentage->%f\n",u1.per);
printf("\ngrade->%c",u1.grade);
getch();
}

WRITE A PROGRAM TO READ AND PRINT THE VALUES OF A STRUCTURE VARIABLE USING STRUCTURE POINTER

#include<stdio.h>
#include<conio.h>
void main()
{
typedef struct student
{
int rollno;
char name[20];
}stud;
stud s1,*p1;
p1=&s1;
printf("enter the roll no and name\n");
scanf("%d%s",&(p1->rollno),p1->name);
printf("rollno->%d name->%s",p1->rollno,p1->name);
getch();
}

A PROGRAMME TO ILLUSTRATE THE CONCEPT OF STRUCTURE POINTER AND POINTER IN STRUCTURE

#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
{
typedef struct info
{
int *sno;
char name[20];
}info;
info s1,*p1;
int i=11;
clrscr();
p1=&s1;
s1.sno=&i;
strcpy(s1.name,"AMITA");
printf("%u",&i);
printf("\nsno->%u name->%s ",(p1->sno),p1->name);
printf("\nsno->%d name->%s ",*(p1->sno),p1->name);
getch();
}

WRITE A PROGRAMME TO STORE THE DETAILS OF AN EMPLOYEE WHERE DETAILS CONSIST OF EMPLOYEE'S SNO,NAME,DOB AND DOJ

#include<stdio.h>
#include<conio.h>
typedef struct date
{
int day;
int mon;
int year;
}date;
typedef struct emp
{
int sno;
char name[20];
date dob,doj;
}emp;
void main()
{
int i,n;
emp e1[10];
clrscr();
printf("enter the no employees\n");
scanf("%d",&n);
printf("enter the details of employee\n");
for(i=0;i<n;i++)
{
printf("enter the sno and name of employee\n");
scanf("%d%s%",&e1[i].sno,e1[i].name);
printf("enter the dob of employee\n");
scanf("%d%d%d",&e1[i].dob.day,&e1[i].dob.mon,&e1[i].dob.year);
printf("enter the doj of employee\n");
scanf("%d%d%d",&e1[i].doj.day,&e1[i].doj.mon,&e1[i].doj.year);
}
printf("the details of employee are \n");
for(i=0;i<n;i++)
{
printf("\n");
printf("sno->%d\nname->%s\n",e1[i].sno,e1[i].name);
printf("dob->%d%d%d\n",e1[i].dob.day,e1[i].dob.mon,e1[i].dob.year);
printf("doj->%d%d%d\n",e1[i].doj.day,e1[i].doj.mon,e1[i].doj.year);
}
getch();
}

WRITE A FUNCTION TO CALCULATE THE AVERAGE OF MARKS OF STUDENT AND THEN PRINT THE RESULT IN CALLING FUNCTION

#include<stdio.h>
#include<conio.h>
typedef struct student
{
char name[20];
int rollno;
int age;
int marks[4];
}stu;
float fun1(int*);
void main()
{
stu s1;
int i;
float avg;
clrscr();
printf("enter the name, rollno and age of student\n");
scanf("%s%d%d",s1.name,&s1.rollno,&s1.age);
printf("enter the marks in 4 subjects\n");
for(i=0;i<4;i++)
{
scanf("%d",&s1.marks[i]);
}
avg=fun1(s1.marks);
printf("the average of %s is= %f",s1.name,avg);
getch();
}

float fun1(int *p)
{
int i,sum=0;
float avg;
for(i=0;i<4;i++)
{
sum=sum+*p;
p++;
}
avg=sum/4.0;
return(avg);
}

WRITE A FUNCTION TO READ THE DETAILS OF A STUDENT AND THEN DISPLAY DETAILS IN CALLING FUNCTION

#include<stdio.h>
#include<conio.h>
typedef struct student
{
char name[20];
int rollno;
int age;
int marks[4];
}stu;
stu fun1(stu);
void main()
{
stu s1,s2;
int i;
clrscr();
s2=fun1(s1);
printf("the details of student is:\n");
printf("name-%s\nrollno-%d\nage-%d",s2.name,s2.rollno,s2.age);
for(i=0;i<4;i++)
{
printf("\nmarks in %d subject is- %d",i+1,s2.marks[i]);
}
getch();
}

stu fun1(stu s1)
{
int i;
printf("enter the name, rollno and age of student\n");
scanf("%s%d%d",s1.name,&s1.rollno,&s1.age);
printf("enter the marks in 4 subjects\n");
for(i=0;i<4;i++)
{
scanf("%d",&s1.marks[i]);
}
return(s1);
}

WRITE A PROGRAMME TO STORE INFORMATION OF N STUDENTS WHERE INFORMATION COMPRISES OF STUDENT'S NAME,SNO AND MARKS IN 4 SUBJECTS

#include<stdio.h>
#include<conio.h>
void main()
{
typedef struct student
{
int sno;
char name[20];
int submarks[4];
}stu;
stu s1[10];
int i,n,j;
clrscr();
printf("enter the no of students\n");
scanf("%d",&n);
printf("enter the details of students\n");
for(i=0;i<n;i++)
{
printf("enter sno and name\n");
scanf("%d%s",&s1[i].sno,s1[i].name);
printf("enter the marks in 4 subjects\n");
for(j=0;j<4;j++)
{
scanf("%d",&s1[i].submarks[j]);
}
}
printf("the details of students are\n");
for(i=0;i<n;i++)
{
printf("sno-%d name-%s\n",s1[i].sno,s1[i].name);
printf("the marks in 4 subjects\n");
for(j=0;j<4;j++)
{
printf("marks in %d subject is %d\n",j,s1[i].submarks[j]);
}
}
getch();
}

WRITE A PROGRAMME TO ILLUSTRATE THE DIFFERENT WAYS OF DECLARATION AND INITIALIZATION OF STRUCTURE VARIABLE BY STORING INFORMATION OF THREE STUDENT

#include<conio.h>
#include<stdio.h>
#include<strin.h>
void main()
{
//Different ways of declaring structure variable
struct info
{
int sno;
char name[20];
char add[20];
}s1,s2;

/*struct
{
int sno;
char name[20];
char add[20];
}s3,s4;*/

//struct info s5,s6;

//Different ways of initialising structure varibles
struct info stu1={1,"ajay","dehradun"};

struct info stu2,stu3;
clrscr();
stu2.sno=2;
//stu2.name="rashmi";//wrong
strcpy(stu2.name,"rashmi");
//printf("\n%u",stu2.name);
//printf("\n%u",stu2.sno);
strcpy(stu2.add,"delhi");

printf("enter the sno,name and add of third student\n");
scanf("%d%s%s",&stu3.sno,stu3.name,stu3.add);

printf("\ndetails of students are\n");
printf("\ndetails of 1st stu-> %d\t%s\t%s",stu1.sno,stu1.name,stu1.add);
printf("\ndetails of 2nd stu-> %d\t%s\t%s",stu2.sno,stu2.name,stu2.add);
printf("\ndetails of 3rd stu-> %d\t%s\t%s",stu3.sno,stu3.name,stu3.add);



getch();
}

WRITE A PROGRAMME TO FIND WHETHER A STRING IS PALINDROME OR NOT

#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
{
char s1[20];
int i=0,l,j,flag;
clrscr();
printf("enter the string\n");
scanf("%s",s1);
l=strlen(s1);
for(i=0,j=l-1;i<j;i++,j--)
{
if(s1[i]==s1[j])
{
flag=1;
}
else
{
flag=0;
break;
}
}
if(flag==1)
printf("\n%s is pallindrome",s1);
else
printf("\n%s is not a pallindrome",s1);
getch();
}

WRITE A PROGRAMME TO IMPLEMENT THE LIBRARY FUNCTIONS-STRLEN,STRCMP,STRLWR,STRUPR,STRREV,STRCPY,STRCAT USING POINTER

//WRITE A PROGRAMME TO IMPLEMENT THE FOLLOWING LIBRARY FUNCTIONS-
//STRLEN,STRCMP,STRLWR,STRUPR,STRREV,STRCPY,STRCAT
//String & Pointer

#include<stdio.h>
#include<conio.h>
int length(char*);
int comp(char*,char*);
void lower(char*);
void upper(char*);
void reverse(char*);
void copy(char*,char*);
void concat(char*,char*);

void main()
{
char s1[20],s2[20],s3[20],s4[20];
int l,i;
clrscr();
printf("enter the first string\n");
gets(s1);
printf("enter the second string\n");
gets(s2);


printf("the length of %s string is- ",s1);
l=length(s1);
printf("%d\n",l);


printf("the result of compairing first and second string-\n");
i=comp(s1,s2);
if(i==0)
{
printf("\n%s and %s are equal\n",s1,s2);
}
else
{
printf("\n %s and %s are not equal with a difference of %d in their ascii value\n",s1,s2,i);
}


printf("\nthe %s string after changing case to lower- ",s1);
lower(s1);
printf("%s\n",s1);


printf("\nthe %s string after changing case to upper- ",s1);
upper(s1);
printf("%s\n",s1);

printf("\n the %s string after being reversed- ",s1);
reverse(s1);
printf("%s\n",s1);


printf("\n %s after being copied by %s is- ",s1,s2);
copy(s1,s2);
printf("%s\n",s1);


printf("enter two strings for applying concatination operation\n");
printf("enter the first string\n");
gets(s3);
printf("enter the second string\n");
gets(s4);
printf("\n%s after being concatenated with %s- ",s3,s4);
concat(s3,s4);
printf("%s\n",s3);


getch();
}

int length(char *p)
{
int i=0;
while(*p)
{
i++;
p++;
}
return(i);
}

int comp(char *p,char *q)
{
int i=0;
while(p[i] && q[i] && (p[i]==q[i]))
{
i++;
}
if(p[i]==q[i])
return 0;
else if(p[i]>q[i])
return(p[i]-q[i]);
else
return(q[i]-p[i]);
}

void lower(char *p)
{
int i=0;
while(p[i])
{
if(p[i]>=65 && p[i]<=91)
 {
   p[i]=p[i]+32;
 }
 i++;
}
}

void upper(char *p)
{
int i=0;
while(p[i])
{
if(p[i]>=97 && p[i]<=123)
 {
   p[i]=p[i]-32;
 }
 i++;
}
}

void reverse(char *p)
{
int i,j,len=strlen(p);
char ch;
for(i=0,j=len-1;i<j;i++,j--)
{
ch=p[i];
p[i]=p[j];
p[j]=ch;
}
}

void copy(char *p,char *q)
{
while(*q)
{
*p=*q;
q++;
p++;
}
if(*p!=0)
*p=0;
}

void concat(char *p,char *q)
{
int i=0,j=0;
while(p[i])
{
i++;
}
p[i]=32;
i++;
while(q[j])
{
p[i]=q[j];
i++;
j++;
}
p[i]='\0';
}

WRITE A PROGRAMME TO COUNT THE NO OF WORDS IN A STRING

#include<conio.h>
#include<stdio.h>
void main()
{
char s1[50];
int i=0,j,c=0;
clrscr();
printf("enter the string\n");
gets(s1);
while(s1[i])
{
if(s1[i]==32)
 {
  c++;
if(s1[i+1]==32)
{
 j=i+1;
 while(s1[j]==32)
 {
    j++;
    continue;
 }
 i=j;
}
  }
i++;
}
printf("no of words in %s are %d",s1,++c);
getch();
}

WRITE A PROGRAMME TO INVERT UPPERCASE LETTERS TO LOWERCASE LETTERS AND VICE VERSA IN A STRING

#include<stdio.h>
#include<conio.h>
void main()
{
char s1[20];
int i=0;
clrscr();
printf("enter the string\n");
gets(s1);
while(s1[i])
{
if(s1[i]>='A'&& s1[i]<='Z')
s1[i]=s1[i]+32;
else if(s1[i]>='a' && s1[i]<='z')
s1[i]=s1[i]-32;
i++;
}
puts(s1);
getch();
}

WRITE A PROGRAMME TO COUNT NO OF VOWELS,CONSONANTS AND NO OF DIGITS IN A STRING

#include<conio.h>
#include<stdio.h>
void main()
{
char ar[20];
int v=0,c=0,d=0,i=0;
clrscr();
printf("enter the string\n");
gets(ar);
while(ar[i])
{
if(ar[i]!=32)
{
if(ar[i]>='0'&&ar[i]<='9')
d++;
else if(ar[i]=='a'||ar[i]=='e'||ar[i]=='i'||ar[i]=='o'||ar[i]=='u')
v++;
else
c++;
}
i++;
}
printf("\nno of vowels in %s are-%d",ar,v);
printf("\nno of consonants in %s are-%d",ar,c);
printf("\nno of digits in %s are-%d",ar,d);
getch();
}

WRITE A PROGRAMME TO COPY ONE STRING INTO ANOTHER STRING

#include<conio.h>
#include<stdio.h>
void main()
{
char s1[20],s2[20];
int i=0;
clrscr();
printf("enter the string\n");
gets(s2);
while(s2[i])
{
s1[i]=s2[i];
i++;
}
s1[i]='\0';
printf("\n%s",s1);
getch();
}

//WRITE A PROGRAMME TO READ AND PRINT A STRING USING SCANF,PRINTF,GETS,PUTS AND %S FORMAT SPECIFIER

#include<conio.h>
#include<stdio.h>
void main()
{
char s1[20],s2[20];
clrscr();
printf("enter the first string\n");
//use of scanf with %s to read string
scanf("%s",s1);
printf("%s",s1);

printf("enter the second string\n");
//use of gets and puts to read and display string
fflush(stdin);
gets(s2);
puts(s2);

getch();
}

Wednesday, May 14, 2014

WRITE A PROGRAMME TO INSERT AN ELEMENT 1-AT THE BEGINNING OF AN ARRAY 2-AT THE END OF AN ARRAY 3-AT A PARTICULAR POSITION IN AN ARRAY

/*WRITE A PROGRAMME TO INSERT AN ELEMENT
1-AT THE BEGINNING OF AN ARRAY
2-AT THE END OF AN ARRAY
3-AT A PARTICULAR POSITION IN AN ARRAY
*/

#include<conio.h>
#include<stdio.h>
void main()
{
int ar[10],i,choice,item,n,pos;
clrscr();
printf("enter the limit of array\n");
scanf("%d",&n);
printf("enter the elements of array\n");
for(i=0;i<n;i++)
{
scanf("%d",&ar[i]);
}
printf("enter the element to be inserted\n");
scanf("%d",&item);
printf("enter the choice\n");
scanf("%d",&choice);
printf("1=INSERT AT THE BEGINING\n2=INSERT AT THE END\n3=INSERT AT PARTICULAR LOCATION\n");
switch(choice)
{
case 1:
for(i=n;i>0;i--)
{
ar[i]=ar[i-1];
}
ar[i]=item;
break;
case 2:
ar[n]=item;
break;
case 3:
printf("enter the position\n");
scanf("%d",&pos);
pos--;
for(i=n;i>pos;i--)
{
ar[i]=ar[i-1];
}
ar[i]=item;
break;
default:
printf("wrong choice");
}
printf("now the elments of array are\n");
for(i=0;i<=n;i++)
{
printf("%d",ar[i]);
}
getch();
}