0% found this document useful (0 votes)
39 views

Ques1: Write A Program To Eliminate Any Word From The String. Ans

The document contains solutions to 9 questions on C programming. Question 1 asks to write a program to remove a substring from a string. Question 2 asks to write a program to multiply two binary numbers. Question 3 asks to make a menu driven program to check if a number is prime recursively and find prime factors recursively. Question 4 asks to write a program using pointers to multiply two matrices. Question 5 asks to write a program to write and read data from a file and perform manipulations on the data. Question 6 asks to search and edit data in a file. Question 7 asks to print the ASCII values of keys on the keyboard. Question 8 asks to sort an array of characters using insertion sort. Question 9 asks to demonstrate the #ifdef

Uploaded by

sam
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

Ques1: Write A Program To Eliminate Any Word From The String. Ans

The document contains solutions to 9 questions on C programming. Question 1 asks to write a program to remove a substring from a string. Question 2 asks to write a program to multiply two binary numbers. Question 3 asks to make a menu driven program to check if a number is prime recursively and find prime factors recursively. Question 4 asks to write a program using pointers to multiply two matrices. Question 5 asks to write a program to write and read data from a file and perform manipulations on the data. Question 6 asks to search and edit data in a file. Question 7 asks to print the ASCII values of keys on the keyboard. Question 8 asks to sort an array of characters using insertion sort. Question 9 asks to demonstrate the #ifdef

Uploaded by

sam
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 16

C Practical

Date: __________

Ques1: Write a program to eliminate any word from the string.


Ans:
#include<conio.h>
#include<stdio.h>
void main()
{
char *str,*ss,*ptr;
printf("Enter a string : ");
gets(str);
printf("\nYou entered : \"%s\"",str);
printf("\n\nEnter a substring u want to remove from above string : ");
gets(ss);
ptr = strstr(str, ss);
if(ptr)
{
*ptr='\0';
ptr+=strlen(ss);
strcat(str,ptr);
printf("\n\nThe string after removing given substring is: \"%s\"\n",str);
}
else
{
printf("\nThe substring not found in given string.");
}
getch();
}

Page 1

C Practical

Ques2: Write a program to multiply two binary numbers.


Ans:
#include<stdio.h>
#include<conio.h>
#define n 5
int e=0,i,c[n],b1[n],q1[n];
ds(int c)
{
int s;
s=c%10;
return (s);
}
shiftwrite()
{
int b;
b=c[n-1];
for(i=n-1;i>0;i--)
{
c[i]=c[i-1];
q1[i]=q1[i-1];
}
c[0]=e;
q1[0]=b;
e=0;
return;
}
sum()
{
for(i=n-1;i>0;i--)
{
if(e==1)
{
if((c[i]==1) && (b1[i]==1))
{
c[i]=1;
e=1;
}
else
{
if((c[i]==0) && (b1[i]==0))
{
c[i]=1;
e=0;
Page 2

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: __________

Ques4: Write a program using pointer to multiply two matrixes.


Ans:
#include<conio.h>
#include<stdio.h>
void mult(int *,int *);
void main()
{
int m1[3][3],m2[3][3];
int i,j;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
scanf("%d",&m1[i][j]);
}
printf("\n");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
scanf("%d",&m2[i][j]);
}
mult(m1,m2);
getch();
}
void mult(int *m1,int *m2)
{
int m3[3][3],i,j;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
m3[i][j]=*(m1+i*3)* *(m2+j)+ *(m1+i*3+1)* *(m2+3+j)+ *(m1+i*3+2)*
*(m2+6+j);
}
printf("\n\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",m3[i][j]);
}
printf("\n");
}
}
Page 8

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

printf("\n\nEnter new Marks Hindi: ");


scanf("%f",&r.hindi);
printf("\nEnter new Marks Computer ");
scanf("%f",&r.comp);
fseek(f,-(long)sizeof(r),1);
fwrite(&r,sizeof(r),1,f);
printf("\n\nUpdated.");
break;
}
}
fclose(f);
getch();
}

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

Ques9: Write a program to demonstrate #ifdef macro and #elif macro.


Ans:
#include<stdio.h>
#define FRND 7
main()
{
#ifdef FRND
printf("Hello Friend");
#else
printf("Hi anyone.");
#endif
#if FRND==7
printf("How are you?");
#elif FRND==10
printf("What is your name?");
#endif
return;
}

Page 16

Date: __________

You might also like