Ques1: Write A Program To Eliminate Any Word From The String. Ans
Ques1: Write A Program To Eliminate Any Word From The String. Ans
Date: __________
Page 1
C Practical
Date: __________
C Practical
}
else
{
c[i]=0;
e=1;
}
}
}
else
{
if((c[i]==1) && (b1[i]==1))
{
c[i]=0;
e=1;
}
else
{
if((c[i]==0) && (b1[i]==0))
{
c[i]=0;
e=0;
}
else
{
c[i]=1;
e=0;
}
}
}
}
return;
}
void main()
{
int b,count,q,j;
clrscr();
count = n;
printf("Enter the value of B:=");
scanf("%d",&b);
printf("Enter the value of Q:=");
scanf("%d",&q);
while(count!=0)
{
Page 3
Date: __________
C Practical
b1[count-1]=ds(b);
b=b/10;
q1[count-1]=ds(q);
q=q/10;
--count;
}
for (j=0;j<n;j++)
{
c[j]=0;
}
clrscr();
printf("\n\t Binary Multiplication Using Booth Algorithm \n");
printf("\n\t-------------------------------------------------\n");
printf("\t Multiplication B=");
for(j=0;j<n;j++)
printf("%d",b1[j]);
printf("\t E \t C \t Q");
printf("\n\t-------------------------------------------------\n");
printf("\t Multiplier in Q");
printf("\t%d\t",e);
for(j=0;j<n;j++)
printf("%d",c[j]);
printf("\t");
for(j=0;j<n;j++)
printf("%d",q1[j]);
count = n;
while (count!=0)
{
if(q1[n-1]==1)
{
sum();
printf("\n");
printf("\tQn=1 : Add B\t\t");
printf("%d\t",e);
for(j=0;j<n;j++)
printf("%d",c[j]);
printf("\t");
for(j=0;j<n;j++)
printf("%d",q1[j]);
shiftwrite();
printf("\n\tShift Right EAQ\t\t");
printf("%d\t",e);
for(j=0;j<n;j++)
Page 4
Date: __________
C Practical
printf("%d",c[j]);
printf("\t");
for(j=0;j<n;j++)
printf("%d",q1[j]);
}
else
{
shiftwrite();
printf("\n");
printf("\tQn=0 shift right EAQ\t");
printf("%d\t",e);
for(i=0;i<n;i++)
printf("%d",c[i]);
printf("\t");
for(i=0;i<n;i++)
printf("%d",q1[i]);
}
count--;
}
printf("\n\n\t\t Answer is AQ = ");
for(i=0;i<n;i++)
printf("%d",c[i]);
printf(" ");
for(i=0;i<n;i++)
printf("%d",q1[i]);
printf("\n\t-------------------------------------------------\n");
getch();
}
Page 5
Date: __________
C Practical
Date: __________
Ques3: Make a menu driven program to find whether the no. is prime or not
recursively and find the prime factor recursively.
Ans:
#include<conio.h>
#include<stdio.h>
void prime(int);
void primefact(int);
void main()
{
int n,c;
clrscr();
while(c!=3)
{
printf("\n\n\n1. Prime number\n2. Prime factor\n3. Exit\nEnter your
choice [ ]\b\b");
scanf("%d",&c);
switch(c)
{
case 1:
printf("Enter a number: ");
scanf("%d",&n);
prime(n);
break;
case 2:
printf("Enter a number: ");
scanf("%d",&n);
primefact(n);
}
}
}
void prime(int n)
{
static int i=2;
if(n%i!=0 && i<=(n/2))
{
i++;
prime(n);
}
else if(i>(n/2))
{
printf("%d is a prime number");
i=2;
}
Page 6
C Practical
else
{
printf("%d is not a prime number");
i=2;
}
}
void primefact(int n)
{
static int i=2;
if(n%i==0)
{
printf("%d, ",i);
n=n/i;
i=2;
if(n>1)
{
primefact(n);
}
}
else
{
i++;
primefact(n);
}
}
Page 7
Date: __________
C Practical
Date: __________
C Practical
Date: __________
Ques5: Write a program to write the data in a file and after reading that data we have
to do some manipulation on that data.
Ans:
#include<conio.h>
#include<stdio.h>
struct record
{
int code;
char name[20];
float sal;
}emp;
void main()
{
FILE *fp;
int ch,cd;
clrscr();
if((fp=fopen("emp.txt","w"))==NULL)
{
printf("cannot open file.");
exit(1);
}
while(1)
{
printf("Enter Employee code(<=0 for EXIT)");
scanf("%d",&emp.code);
if(emp.code<=0)break;
printf("Enter employee name");
fflush(stdin);
gets(emp.name);
printf("Enter employee salary");
scanf("%f",&emp.sal);
fwrite(&emp,sizeof(emp),1,fp);
}
fclose(fp);
fp=fopen("emp.txt","r");
printf("\n\nRecords are:\n\n");
while((fread(&emp,sizeof(emp),1,fp))>0)
{
printf("\nCode: %d",emp.code);
printf("\tName: %s",emp.name);
printf("\tSalary: %f",emp.sal);
}
fclose(fp);
Page 9
C Practical
Date: __________
while(1)
{
printf("\n\nEnter your choice:-");
printf("\n1. add records\n2. Modify record\n3. Dispaly record\n4. Display all
records\n5. exit");
scanf("%d",&ch);
switch(ch)
{
case 1:
fp=fopen("emp.txt","a");
printf("Enter Employee code");
scanf("%d",&emp.code);
if(emp.code<=0)break;
printf("Enter employee name");
fflush(stdin);
gets(emp.name);
printf("Enter employee salary");
scanf("%f",&emp.sal);
fwrite(&emp,sizeof(emp),1,fp);
fclose(fp);
break;
case 2:
printf("Enter employee code to modify record: ");
scanf("%d",&cd);
fp=fopen("emp.txt","r+");
while((fread(&emp,sizeof(emp),1,fp))>0)
{
if(emp.code==cd)
{
printf("\nrecord found");
printf("\nCode: %d\nName: %s\nSalary: %f",emp.code,emp.name,emp.sal);
printf("\nEnter new Code: ");
scanf("%d",&emp.code);
printf("Enter employee name");
fflush(stdin);
gets(emp.name);
printf("Enter employee salary");
scanf("%f",&emp.sal);
fseek(fp,-(long)sizeof(emp),1);
fwrite(&emp,sizeof(emp),1,fp);
printf("\nRecord modified\n");
break;
Page 10
C Practical
Date: __________
}
}
fclose(fp);
break;
case 3:
printf("Enter employee code to display: ");
scanf("%d",&cd);
fp=fopen("emp.txt","r");
while((fread(&emp,sizeof(emp),1,fp))>0)
{
if(emp.code==cd)
{
printf("\nrecord found");
printf("\nCode: %d\nName: %s\nSalary: %f",emp.code,emp.name,emp.sal);
break;
}
}
fclose(fp);
break;
case 4:
fp=fopen("emp.txt","r");
while((fread(&emp,sizeof(emp),1,fp))>0)
{
printf("\nCode: %d\nName: %s\nSalary: %f",emp.code,emp.name,emp.sal);
}
fclose(fp);
break;
case 5:
exit(0);
}
}
getch();
}
Page 11
C Practical
Date: __________
Ques6: Write a program to search any data from the file and edit that data and write
the edit form into the file.
Ans:
#include<conio.h>
#include<stdio.h>
struct rec
{
int rno;
float hindi;
float comp;
}r;
void main()
{
int rn;
FILE *f;
if((f=fopen("data.dat","r+"))==NULL)
{
f=fopen("data.dat","w");
printf("Enter records.\n");
while(1)
{
printf("\nRoll No. : ");
scanf("%d",&r.rno);
printf("Marks:\n\tHindi : ");
scanf("%f",&r.hindi);
printf("\tComputer : ");
scanf("%f",&r.comp);
if(r.rno<=0)break;
fwrite(&r,sizeof(r),1,f);
}
fclose(f);
f=fopen("data.dat","r+");
}
printf("Enter Roll No. of a student to search: ");
scanf("%d",&rn);
while((fread(&r,sizeof(r),1,f))>0)
{
if(r.rno==rn)
{
printf("Student's current details : \n");
printf("Roll No. %d",r.rno);
printf("\nMarks Hindi: %f",r.hindi);
printf("\nMarks Computer %f ",r.comp);
Page 12
C Practical
Page 13
Date: __________
C Practical
Date: __________
Ques7: Write a program to print the ASCII value of the keys given in the keyboard.
Ans:
#include<conio.h>
#include<stdio.h>
main()
{
int i;
clrscr();
printf("Character\t\tASCII Value\t Character\t\tASCII Value");
for(i=32;i<=125;i++)
{
printf("\n %c\t -->\t\t%d",i,i);
i++;
printf("\t\t %c\t -->\t\t%d",i,i);
}
getch();
return;
}
Page 14
C Practical
Date: __________
Ques8: Write a program to sort the array of characters using insertion sort.
Ans:
#include<conio.h>
#include<stdio.h>
void main()
{
char ch[10],t;
int i,j;
clrscr();
for(i=0;i<10;i++)
{
printf("Enter a character(%d): ",i);
scanf("%c",&ch[i]);
fflush(stdin);
}
for(i=0;i<9;i++)
{
for(j=i+1;j<10;j++)
{
if(ch[i]>ch[j])
{
t=ch[i];
ch[i]=ch[j];
ch[j]=t;
}
}
}
printf("\nArray after sorting:\n");
for(i=0;i<10;i++)
{
printf("\n(%d): %c",i,ch[i]);
}
}
Page 15
C Practical
Page 16
Date: __________