# 1

#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10],b[10][10],c[10][10],i,j,r1,c1,r2,c2;
clrscr();
printf("enter r1 c1\n");
scanf("%d%d",&r1,&c1);
printf("enter r2 c2\n");
scanf("%d%d",&r2,&c2);
if((r1==r2)&&(c1==c2))
{
printf("enter in first matrix\n");
for(i=1;i<=r1;i++)
for(j=1;j<=c1;j++)
scanf("%d",&a[i][j]);
printf("enter in second matrix\n");
for(i=1;i<=r2;i++)
for(j=1;j<=c2;j++)
scanf("%d",&b[i][j]);
for(i=1;i<=r1;i++)
{
for(j=1;j<=c1;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
for(i=1;i<=r1;i++)
{
for(j=1;j<=c1;j++)
{
printf("%4d",c[i][j]);
}
printf("\n");
}
}
else
Department of Computers

2

getch();
}
/*
input
enter r1 c1
2 2
enter r2
2 2
enter in
1 2
3 4
enter in
1 5
3 6
2 7
6 10

Department of Computers

possible\n");

c2
first matrix
second matrix
OF MATRIX
*/

3

/* ADDITION OF EACH ROW AND COLUMN MATRIX*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10],i,j,r,c,s=0;
clrscr();
printf("enter r and c\n");
scanf("%d%d",&r,&c);
printf("enter in array\n");
for(i=1;i<=r;i++)
for(j=1;j<=c;j++)
scanf("%d",&a[i][j]);
r++;
c++;
for(i=r,j=1;j<c;j++)
a[i][j]=s;
for(j=c,i=1;i<r;i++)
a[i][j]=s;
for(i=1;i<=r;i++)
{
s=0;
for(j=1;j<=c;j++)
{
s=s+a[i][j];
if(j==c)
a[i][j]=s;
}
}
for(j=1;j<=c;j++)
{
s=0;
for(i=1;i<=r;i++)
{
s=s+a[i][j];
if(i==r)
a[i][j]=s;
}
}
Department of Computers

4

printf("printed\n");
for(i=1;i<r;i++)
{
for(j=1;j<=c;j++)
{
printf("%4d",a[i][j]);
}
printf("\n");
}
i=r;
for(j=1;j<c;j++)
printf("%4d",a[i][j]);
getch();
}
/* input
enter r and c
2 2
enter in array
1 2
3 4
printed
1
2
3
3
4
7
4
6
*/

Department of Computers

5

/* AMSTRONG NUMBER */
#include<stdio.h>
#include<conio.h>
void main()
{
int n,r,s=0,m;
clrscr();
printf("enter any number\n");
scanf("%d",&n);
m=n;
while(n>0)
{
r=n%10;
s=s+r*r*r;
n/=10;
}
if(s==m)
printf("IT IS ARMSTRONG\n");
else
printf("IT IS NOT ARMSTRONG\n");
getch();
}
/* input
enter any number
153
IT IS AMSTRONG
*/

Department of Computers

6

/* BIGGEST OF 3 NUMBERS */
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,d;
clrscr();
printf(" enter a,b,c values\n");
scanf("%d%d%d",&a,&b,&c);
d=((a>b)&&(a>c))?a:(b>c)?b:c;
printf("biggest=%d",d);
getch();
}
/* input
enter a,b,c values
2 3 4
biggest=4
*/

Department of Computers

7

/* BIGGEST OF N NUMBERS */
#include<stdio.h>
#include<conio.h>
void main()
{
static int i,n,big,a[100];
clrscr();
printf("enter range\n");
scanf("%d",&n);
printf("enter in array\n");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
big=a[1];
for(i=2;i<=n;i++)
if(a[i]>a[1])
big=a[i];
printf("biggest=%d",big);
getch();
}
/* input
enter range
4
enter in array
7
3
9
1
biggest=9
*/

Department of Computers

8

/* BINARY SEARCH METHOD */
#include<stdio.h>
#include<conio.h>
void main()
{
int i,h,l=1,n,a[100],mid,e;
clrscr();
printf("enter n \n");
scanf("%d",&n);
printf("enter into array in ascending\n");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
printf("enter element\n");
scanf("%d",&e);
h=n;
mid=(l+h)/2;
while((l<h)&&(a[mid]!=e))
{
if(a[mid]>e)
h=mid-1;
else
if(a[mid]<e)
l=mid+1;
mid=(l+h)/2;
}
if(a[mid]==e)
printf("FOUND AT %d",mid);
else
getch();
}
/* input
enter n
5
enter in array in ascending
11 12 13 14 15
enter element
13
FOUND AT 3
*/
Department of Computers

9

/*BUBBLE SORTING */
#include<stdio.h>
#include<conio.h>
void main()
{
int a[100],i,j,t,n;
clrscr();
printf("enter n value\n");
scanf("%d",&n);
printf("enter in array\n");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
for(i=1;i<n;i++)
{
for(j=i+1;j<=n;j++)
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
printf("after sorting\n");
for(i=1;i<=n;i++)
printf("%4d",a[i]);
getch();
}
/* input
enter n value
6
enter in array
9
45
8
2
5
6
after sorting
2
5
6
8
Department of Computers

9

45

*/

10

/* CALL BY REFERENCE */
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
void swap(int *,int *);
clrscr();
printf("enter a ,b values\n");
scanf("%d%d",&a,&b);
swap(&a,&b);
printf("AFTER SORTING\n");
printf("%4d %4d",a,b);
getch();
}
void swap(int *x,int *y)
{
int t;
t=*x;
*x=*y;
*y=t;
}
/* input
enter a,b values
2 3
AFTER SORTING
3
2
*/

Department of Computers

11

/* COMMAND LINE ARGUMENT */
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main(int argc,char *argv[])
{
int n,f=1;
clrscr();
if(argc<2)
{
printf("data is wrong\n");
exit(0);
}
else
n=atoi(argv[1]);
while(n>1)
{
f=f*n;
n--;
}
printf("fact=%d",f);
getch();
}
/* input
c:\tc\bin>cla 5
fact=120
*/

Department of Computers

12

/* COMPOUND*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float a,ci,p,t,r;
int n;
clrscr();
printf("enter p,t,r,n values\n");
scanf("%f%f%f%d",&p,&t,&r,&n);
a=p*(double)pow((1+(r/(n*100))),(double)(n*t));
ci=a-p;
printf("ci=%f",ci);
getch();
}
/* input
enter p,t,r,n values
2 4 1 12
ci=0.081587
*/

Department of Computers

13

/* COPY ONE FILE TO ANOTHER FILE */
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main()
{
FILE *f1,*f2;
char c,k;
clrscr();
f1=fopen("sai","w");
printf("ENTER INTO FILE\n");
while((c=getchar())!=EOF)
putc(c,f1);
fclose(f1);
f1=fopen("sai","r");
f2=fopen("duplicate","w");
while((c=getc(f1))!=EOF)
putc(c,f2);
fclose(f1);
fclose(f2);
f2=fopen("duplicate","r");
printf("COPIED FILE\n");
while((c=getc(f1))!=EOF)
{
k=toupper(c);
printf("%c",k);
}
fclose(f2);
getch();
}
/* input
ENTER INTO FILE
sai is god
^Z
COPIED FILE
SAI IS GOD
*/

Department of Computers

14

/* SUM OF THE DIGITS IN A GIVEN NUMBER */
#include<stdio.h>
#include<conio.h>
void main()
{
int n,r,s=0;
clrscr();
printf("enter n value\n");
scanf("%d",&n);
while(n>0)
{
r=n%10;
s=s+r;
n/=10;
}
printf("sum=%d",s);
getch();
}
/* input
enter n value
123
sum=6
*/

Department of Computers

15

/* EVEN ODD */
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *f1,*f2,*f3;
int n,i;
clrscr();
f1=fopen("data","w");
printf("enter in file1\n");
for(i=1;i<=100;i++)
{
scanf("%d",&n);
if(n==-1)
break;
else
putw(n,f1);
}
fclose(f1);
f1=fopen("data","r");
f2=fopen("even","w");
f3=fopen("odd","w");
while((n=getw(f1))!=EOF)
{
if(n%2==0)
putw(n,f2);
else
putw(n,f3);
}
fclose(f1);
fclose(f2);
fclose(f3);
printf("EVEN NOS\n");
f2=fopen("even","r");
while((n=getw(f2))!=EOF)
printf("%4d",n);
fclose(f2);
printf("\n");
printf("ODD NOS\n");
Department of Computers

16

f3=fopen("odd","r");
while((n=getw(f3))!=EOF)
printf("%4d",n);
fclose(f3);
getch();
}
/* input
enter in file1
4
5
7
3
2
-1
EVEN NOS
4 2
ODD NOS
5 7 3
*/

Department of Computers

17

/* CREATE A FILE BY USING fwrite() */
#include<stdio.h>
#include<conio.h>
void main()
{
struct bank
{
int acno,amount;
char name[40];
}b;
FILE *f;
clrscr();
f=fopen("andhra","ab+");
if(f==NULL)
{
gotoxy(20,12);
printf("enter number=");
gotoxy(55,12);
scanf("%d",&b.acno);
gotoxy(20,13);
printf("enter name=");
gotoxy(55,13);
scanf("%s",b.name);
gotoxy(20,14);
printf("enter amount=");
gotoxy(55,14);
scanf("%d",&b.amount);
fwrite(&b,sizeof(b),1,f);
}
else
{
clrscr();
gotoxy(20,12);
printf("enter number=");
gotoxy(55,12);
scanf("%d",&b.acno);
gotoxy(20,13);
printf("enter name=");
Department of Computers

18

gotoxy(55,13);
scanf("%s",b.name);
gotoxy(20,14);
printf("enter amount=");
gotoxy(55,14);
scanf("%d",&b.amount);
fwrite(&b,sizeof(b),1,f);
}
rewind(f);
fclose(f);
}

Department of Computers

19

/* FIBBANACIS SERIES */
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i=2,f1=0,f2=1,f3;
clrscr();
printf("ENTER N VALUE\n");
scanf("%d",&n);
printf("%4d%4d",f1,f2);
while(i<n)
{
f3=f1+f2;
printf("%4d",f3);
f1=f2;
f2=f3;
i++;
}
getch();
}
/* input
ENTER N VALUE
7
0 1 1 2 3

Department of Computers

5

8

*/

20

/* fseek() ftell() */
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *f;
char c;
long int n,co,t;
clrscr();
f=fopen("sai","w");
printf("enter a line\n");
while((c=getchar())!='\n')
putc(c,f);
printf("number=%ld\n ",ftell(f));
t=ftell(f);
fclose(f);
f=fopen("sai","r");
n=0l;
while(feof(f)==0)
{
fseek(f,n,0);
printf("position %c is %ld\n",getc(f),ftell(f));
n=n+1l;
}
co=0;
n=-1l;
fseek(f,-1l,2);
do
{
printf("%c",getc(f));
fseek(f,n-1l,1);
co++;
}
while(co<t);
fclose(f);
getch();
}
/* input
enter a line
Department of Computers

21

kumarsir
number=8
position
position
position
position
position
position
position
position
position
risramuk

Department of Computers

k
u
m
a
r
s
i
r

is 0
is 1
is 2
is 3
is 4
is 5
is 6
is 7
is 8
*/

22

/*INSERT AN ELEMENT IN ARRAY*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a[100],i,n,e,p;
clrscr();
printf("enter n value\n");
scanf("%d",&n);
printf("enter in array\n");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
printf("enter e and p\n");
scanf("%d%d",&e,&p);
n++;
for(i=n;i>p;i--)
a[i]=a[i-1];
a[p]=e;
printf("after inserting\n");
for(i=1;i<=n;i++)
printf("%4d",a[i]);
getch();
}
/* input
enter n value
4
enter in array
1 3 4 5
enter e and p
2 2
after inserting
1 2 3 4 5

Department of Computers

*/

23

/* INSERTION SORT */
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,a[100],n,t;
clrscr();
printf("enter n value\n");
scanf("%d",&n);
printf("enter in array\n");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
for(i=2;i<=n;i++)
{
for(j=i-1;j>=1;j--)
{
if(a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
printf("AFTER SORTING\n");
for(i=1;i<=n;i++)
printf("%4d",a[i]);
getch();
}
/* input
enter n value
4
enter in array
7 4 2 6
AFTER SORTING
2 4 6 7

Department of Computers

*/

24

/* LENGTH OF THE STRING IN POINTERS */
#include<stdio.h>
#include<conio.h>
void main()
{
char *p,s[80];
clrscr();
p=s;
printf("enter\n");
while((*p++=getchar())!='\n');
*--p='\0';
printf("%s\n",s);
printf("length=%d",(p-s));
getch();
}
In put:
Output:

enter
Kumar
length=5

Department of Computers

25

/* LINEAR SEARCH */
#include<stdio.h>
#include<conio.h>
void main()
{
int a[100],i,n,e,f=0;
clrscr();
printf("enter n value\n");
scanf("%d",&n);
printf("enter in array\n");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
printf("enter element\n");
scanf("%d",&e);
i=1;
while((i<=n)&&(f==0))
{
if(a[i]==e)
{
f=1;
break;
}
i++;
}
if(f)
printf("FOUND");
else
getch();
}
/* input
enter n value
5
enter in array
9 6 7 2 10
enter element
10
FOUND
*/

Department of Computers

26

/* MULTIPLICATION OF MATRIX IN POINTERS */
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
void main()
{
int *a[10],*b[10],*c[10],i,j,k,r1,c1,r2,c2;
void mul();
void show();
clrscr();
printf("enter r1,c1\n");
scanf("%d%d",&r1,&c1);
printf("enter r2,c2\n");
scanf("%d%d",&r2,&c2);
if(c1!=r2)
{
printf("MATRIX MUTIPLICATION IS NOT POSSIBLE\n");
}
else
{
for(i=0,j=0;i<r1,j<r2;i++,j++)
{
a[i]=(int *)malloc(c1*sizeof(int));
b[j]=(int *)malloc(c2*sizeof(int));
c[i]=(int *)malloc(c2*sizeof(int));
}
printf("enter in array\n");
printf("enter in array\n");
mul(a,b,c,r1,c2,c1);
show(c,r1,c2);
}
getch();
}
{
Department of Computers

27

int i,j;
for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf("%d",x[i]+j);
}
void mul(int *a[10],int *b[10],int *c[10],int r1,int
c2,int c1)
{
int i,j,k;
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
*(c[i]+j)=0;
for(k=0;k<c1;k++)
{
*(c[i]+j)+=(*(a[i]+k)*(*(b[k]+j)));
}
}
}
}
void show(int *x[10],int r,int c)
{
int i,j;
printf(" AFTER MULTIPLICATION\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%5d",*(x[i]+j));
}
printf("\n");
}
}
/* input
enter r1,c1
2 2
enter r2,c2
2 2
enter in array
Department of Computers

28

4 5
2 8
enter
1 0
0 1
AFTER
4
2

Department of Computers

in array
MULTIPLICATION
5
8
*/

29

/* MERGING OF TWO FILES*/
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *f1,*f2,*f3;
char c;
clrscr();
f1=fopen("sai","w");
printf("enter into file1\n");
while((c=getchar())!=EOF)
putc(c,f1);
fclose(f1);
f2=fopen("ram","w");
printf("enter into file2\n");
while((c=getchar())!=EOF)
putc(c,f2);
fclose(f2);
f1=fopen("sai","r");
f2=fopen("ram","r");
f3=fopen("sairam","w");
while((c=getc(f1))!=EOF)
putc(c,f3);
fclose(f1);
while((c=getc(f2))!=EOF)
putc(c,f3);
fclose(f2);
fclose(f3);
f3=fopen("sairam","r");
printf("AFTER MERGING INTO FILE 3\n");
while((c=getc(f3))!=EOF)
printf("%c",c);
fclose(f3);
getch();
}
/* input
enter into file1
SAI
^Z
Department of Computers

30

enter into file2
RAM
^Z
AFTER MERGING INTO FILE3
SAI
RAM */

Department of Computers

31

/*NAMES SORTING*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int n=0,j,i;
char x[20][10],t[10];
clrscr();
printf("enter the string up to end\n");
do
{
scanf("%s",x[n]);
}
while(strcmp(x[n++],"end"));
n--;
for(i=0;i<n;i++)
{
for(j=i+1;j<=n;j++)
{
if(strcmp(x[i],x[j])>0)
{
strcpy(t,x[i]);
strcpy(x[i],x[j]);
strcpy(x[j],t);
}
}
}
printf("after sorting\n");
for(i=0;i<n;i++)
printf("%s\n",x[i]);
getch();
}
/*input
enter the string upto end
dog
cat
bat
apple
Department of Computers

32

end
after sorting
apple
bat
cat
dog
*/

Department of Computers

33

/* PASCAL TRIANGLE */
#include<stdio.h>
#include<conio.h>
void main()
{
int p,q=0,b,r,x;
clrscr();
printf("enter how many\n");
scanf("%d",&p);
while(q<=p)
{
for(r=40-(3*q);r>0;r--)
printf(" ");
for(x=0;x<=q;x++)
{
if((x==0)||(q==0))
b=1;
else
b=(b*(q-x+1))/x;
printf("%6d",b);
}
printf("\n");
q++;
}
getch();
}

/* input
enter how many
4
1
1
1
1
2
1
1
3
3
1
1
4
6
4
1

Department of Computers

*/

34

/* x to the POWER of y */
#include<stdio.h>
#include<conio.h>
void main()
{
int i,y;
double x,res=1;
clrscr();
printf("enter base power\n");
scanf("%lf%d",&x,&y);
if(y==0)
res=1;
else
if(y>0)
{
i=1;
while(i<=y)
{
res*=x;
i++;
}
}
else
{
i=y;
while(i<0)
{
res/=x;
i++;
}
}
printf("%lf power of %d is %lf",x,y,res);
getch();
}
/* input
enter base power
2 3
2.000000 power of 3 is 8.000000 */

Department of Computers

35

/* PRIME NUMBER GENERATION */
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1,n,r,k,c;
clrscr();
printf("enter any number\n");
scanf("%d",&n);
while(i<=n)
{
k=1;
c=0;
while(k<=i)
{
r=i%k;
if(r==0)
c++;
k++;
}
if(c==2)
printf("%d\t",i);
i++;
}
getch();
}
/*
input
enter any number
10
2 3 5 7
*/

Department of Computers

36

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int k;
float a,b,c,d,x,y,r1,r2;
clrscr();
printf("enter a b c values\n");
scanf("%f%f%f",&a,&b,&c);
if(a==0)
printf("LINEAR EQUATION");
else
{
d=(b*b)-(4*a*c);
if(d>0)
k=1;
else
if(d<0)
k=2;
else
if(d==0)
k=3;
switch(k)
{
case 1:
printf("ROOTS ARE REAL AND UNEQUAL\n");
r1=-b+sqrt(d)/(2*a);
r2=-b-sqrt(d)/(2*a);
printf("root1=%5.2f\n",r1);
printf("root2=%5.2f\n",r2);
break;
case 2:
printf("ROOTS ARE IMAGINARY\n");
x=-b/2*a;
y=-(sqrt(d))/2*a;
printf("root1=%5.2f+i%5.2f\n",x,y);
Department of Computers

37

printf("root2=%5.2f-i%5.2f\n",x,y);
break;
case 3:
printf("ROOTS ARE REAL AND EQUAL");
r1=-b/2*a;
printf("root1=root2=%5.2f\n",r1);
break;
default:
break;
}
}
getch();
}
/* input
enter a b c values
1 4 4
ROOTS ARE REAL AND EQUALroot1=root2=2.00
*/

Department of Computers

38

/* QUICK SORT */
#include<stdio.h>
#include<conio.h>
void main()
{
void quick(int b[20],int,int);
int a[20],i,n;
clrscr();
printf("enter n value\n");
scanf("%d",&n);
printf("enter in array\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
quick(a,0,n-1);
printf(" AFTER SORTING\n");
for(i=0;i<n;i++)
printf("%4d",a[i]);
getch();
}
void quick(int a[20],int l, int r)
{
int i=l,j=r+1,t,p=a[l];
if(l<r)
{
while(1)
{
do
{
i=i+1;
}
while(a[i]<p);
do
{
j=j-1;
}
while(a[j]>p);
if(i>=j)
break;
else
Department of Computers

39

{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
a[l]=a[j];
a[j]=p;
quick(a,l,j-1);
quick(a,j+1,r);
}
}
/* input
enter
8
enter
8 9 4
AFTER
2 3 4

Department of Computers

n value
in array
10 3 7 2 5
SORTING
5 7 8 9 10 */

40

/* SELECTION SORT */
#include<stdio.h>
#include<conio.h>
void main()
{
int j,n,k,min,i,p,a[100];
clrscr();
printf("enter n value\n");
scanf("%d",&n);
printf("enter in array\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
p=0;
while(p<n-1)
{
min=p;
for(j=p+1;j<n;j++)
{
if(a[min]>a[j])
min=j;
}
if(min!=p)
{
k=a[p];
a[p]=a[min];
a[min]=k;
}
p++;
}
printf(" AFTER SORTING\n");
for(i=0;i<n;i++)
printf("%4d",a[i]);
getch();
}
/* input
enter n value
enter in array
6 8 3 9
AFTER SORTING
Department of Computers

4
3 6 8 9

*/

41

/* SMALLEST OF N NUMBERS IN POINTERS */
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],*p,n,i,s;
clrscr();
printf("enter n vlaue\n");
scanf("%d",&n);
printf("enter into array\n");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
p=a;
s=*p;
p++;
for(i=2;i<=n;i++)
{
if(s>*p)
s=*p;
p++;
}
printf("smallest=%d",s);
getch();
}
/* input
enter n value
4
enter into array
7 5 9 4
smallest=4

Department of Computers

*/

42

/* STRUCTURES IN POINTERS */
#include<stdio.h>
#include<conio.h>
void main()
{
struct student
{
int no;
char name[20];
char gender;
int height;
}*x;
clrscr();
printf("enter no,name,gender,height\n");
scanf("%d",&x->no);
scanf("%s",x->name);
fflush(stdin);
scanf("%c",&x->gender);
scanf("%d",&x->height);
printf("%d\n",x->no);
printf("%s\n",x->name);
printf("%c\n",x->gender);
printf("%d\n",x->height);
getch();
}
/* input
enter no,name,gender,height
2
sai
m
6
output
2
sai
m
6
*/

Department of Computers

43

/* Truncating the Given text */
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[80];
int i=0,j,k;
clrscr();
printf("enter a sentence\n");
gets(a);
k=strlen(a);
while(i<k)
{
if(a[i]==' ')
{
j=i+1;
if(a[j]==' ')
{
k--;
while(i<k)
{
a[i]=a[i+1];
i++;
}
i=0;
}
j=0;
}
i++;
}
a[i]='\0';
printf("after string \n %s\n",a);
getch();
}
/* input
enter a sentence
sai
is
after string
sai is god */
Department of Computers

god

44

/* STRING CONCATINATION IN POINTERS */
#include<stdio.h>
#include<conio.h>
#define length 10
#include<malloc.h>
void main()
{
char *s1,*s2,*s3,c;
int i,k,n;
clrscr();
s1=(char*)malloc(length*sizeof(char));
s2=(char*)malloc(length*sizeof(char));
s3=(char*)malloc(length*sizeof(char));
printf("enter first string\n");
gets(s1);
fflush(stdin);
printf("enter second string\n");
gets(s2);
fflush(stdin);
i=0;
while((c=*(s1+i))!='\0')
{
s3[i]=c;
i++;
}
k=0;
while((c=*(s2+k))!='\0')
{
s3[i+k]=c;
k++;
}
n=i+k;
for(i=0;i<n;i++)
printf("%c",s3[i]);
getch();
}
/*

input

Department of Computers

45

enter first string
kumar
enter second string
sir
kumarsir
*/

Department of Computers

46

/* STRONG OR NOT */
#include<stdio.h>
#include<conio.h>
void main()
{
int n,r,f,s=0,m;
clrscr();
printf("enter any number\n");
scanf("%d",&n);
m=n;
while(n>0)
{
f=1;
r=n%10;
while(r>=1)
{
f=f*r;
r=r-1;
}
n/=10;
s+=f;
}
if(s==m)
printf("IT IS STRONG NUMBER\n");
else
printf("IT IS NOT STRONG\n");
getch();
}
/* input
enter any number
145
IT IS STRONG NUMBER

Department of Computers

*/

47

/* STRING PALLINDROM*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int i,j,k,l,f=1;
char x[80];
clrscr();
printf("enter a string\n");
scanf("%s",x);
l=strlen(x);
i=0;
j=l-1;
k=j/2;
while((i<k)&&(f==1))
{
if(x[i]==x[j])
{
i++;
j--;
}
else
f=0;
}
if(f)
printf("IT IS STRING PALLINDROM\n");
else
printf("IT IS NOT A PALLINDROM\n");
getch();
}
/* input
enter a string
IT IS STRING PALLINDROM

Department of Computers

*/

48

/* substring or not */
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int i=0,j=0;
char a[80],b[80];
clrscr();
printf("enter the first string\n");
gets(a);
printf("enter the second string\n");
gets(b);
while(i<strlen(a))
{
if(a[i]==b[j])
{
j++;
if(j==strlen(b))
{
printf("found at %d",i-strlen(b)+2);
break;
}
}
else
j=0;
i++;
}
if(i==strlen(a))
getch();
}
/* input
enter the first string
sai is god
enter the second string
is
found at 5 */

Department of Computers

49

/* SUM OF N NUMBERS */
#include<stdio.h>
#include<conio.h>
void main()
{
int a[100],n,s=0,i;
clrscr();
printf("enter n value\n");
scanf("%d",&n);
printf("enter into array\n");
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
s=s+a[i];
}
printf("%d",s);
getch();
}
/* input
enter n value
4
enter into array
2 3 4 5
14 */

Department of Computers

50

/* Symmetric or not */
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10],b[10][10],i,j,n,f=1;
clrscr();
printf("enter n value\n");
scanf("%d",&n);
printf("enter in array\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf(" %d",&a[i][j]);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
b[i][j]=a[j][i];
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(a[i][j]!=b[i][j])
{
f=0;
break;
}
if(f)
printf("symmetric");
else
printf("not symmetric");
getch();
}
/* input
enter n value
2
enter in array
1 0
0 1
symmetric
*/

Department of Computers

51

/* TOWERS OF HONAI */
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
char big='a',acc='b',end='c';
void tower(int, char, char, char);
clrscr();
printf("enter how many\n");
scanf("%d",&n);
tower(n,big,acc,end);
getch();
}
void tower(int n,char big,char acc,char end)
{
if(n==1)
{
printf("%c->%c\n",big ,end);
return;
}
else
{
tower(n-1,big,end,acc);
printf("%c->%c\n",big,end);
tower(n-1,acc,big,end);
}
}
/* input
enter how many
3
a->c
a->b
c->b
a->c
b->a
b->c
a->c
*/

Department of Computers

52

/* VOWELS

& CONSONANTS */

#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main()
{
char x[88],c;
int i,v,co,w,d,ot;
i=v=co=w=d=ot=0;
clrscr();
printf("enter a sentence\n");
scanf("%[^\n]",x);
while((c=tolower(x[i++]))!='\0')
{
if((c=='a')||(c=='e')||(c=='i')||(c=='o')||(c=='u'))
v++;
else
if((c>'a')&&(c<'z'))
co++;
else
if((c>='0')&&(c<='9'))
d++;
else
if(c==' ')
w++;
else
ot++;
}
++w;
printf("no of vowels=%d\n",v);
printf("no of consonants=%d\n",co);
printf("words=%d\n",w);
printf("digits=%d\n",d);
printf("others=%d\n",ot);
getch();
}
/* input
enter a sentence
phone number:9848660520
Department of Computers

53

no of vowels=4
no of consonants=7
words=2
digits=10
others=1
*/

Department of Computers

54

//sin(x) series
#include<stdio.h>
#include<conio.h>
main()
{
float i,j,n,x,sum=0,term,x1;
clrscr();
printf("\n enter the value for x (in degrees):");
scanf("%f",&x);
x1=x;
printf("\n enter the number of terms:");
scanf("%f",&n);
x=(x*3.14285)/180;
term=x;
for(i=1;i<=n;i++)
{
sum=sum+term;
term=term*(-(x*x)/((2*i)*(2*i+1)));
}
printf("\n the result is sin(%.2f)=%f",x1,sum);
getch();
}

Department of Computers

55

//to print a number in the text form
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<math.h>
void main()
{
char
ch1[20][10]={"zero","one","two","three","four","five","si
x","seven","eight",
"nine","ten","eleven","twelwe","thirteen","fourteen","fif
teen",
"sixteen","seventeen","eighteen","ninteen"};
char
ch2[20][10]={"zero","ten","twenty","thirty","fourty","fif
ty","sixty",
"seventy","eighty","ninty"};
char ch3[5][18]={" ","hundrended
and","thousand","lakh","crore"};
char ch4[120]=" ";
long int n,i,j,m,p,q,val;
clrscr();
printf("\n enter the number :");
scanf("%ld",&n);
for(m=n,i=0;m>0;m/=10,i++);
for(; i>0;i--)
{
val=pow(10,i-1);
m=n/val;
p=((n*10)/val);
q=p%10;
if((i%2==1)&&(m!=0))
{
if((i==3)||(i==1))
{
Department of Computers

56

strcat(ch4,ch1[m]);
strcat(ch4,ch3[i/2]);
}
else
{
if(p>=20)
strcat(ch4,ch2[m]);
else if(p<20)
strcat(ch4,ch1[p]);
if((p<20)||(q==0))
{
strcat(ch4,ch3[i/2]);
i--;
n=n-m*val;
val=pow(10,i-1);
m=n/val;
}
}

}
else if((i%2==0)&&(m!=0))
{
if(i==2)
if(p<20)
{
strcat(ch4,ch1[p]);
break;
}
else
strcat(ch4,ch2[m]);
else
{
strcat(ch4,ch1[m]);
strcat(ch4,ch3[i/2]);
}
}
n=n-m*val;

}
strcat(ch4,"only.");
printf("%s",ch4);
Department of Computers

57

getch();
}

Department of Computers

58

//program to convert decimal to base formate
#include<string.h>
void convert(int n,char *s,int base)
{
int i;
char *num="0123456789abcdef";
clrscr();
for(i=0;n>0;i++)
{
s[i]=num[n%base];
n=n/base;
}
s[i]='\0';
strrev(s);
}
main()
{
int n,base;
char str[20];
printf("\n enter A number :");
scanf("%d",&n);
printf("\n enter the base :");
scanf("%d",&base);
convert(n,str,base);
printf("\n %d base equivalent of %d=%s",base,n,str);
getch();
}

Department of Computers

59

//to print roman numbers
#include<stdio.h>
void main()
{
char ch[]={'i','v','x','l','c','d','m'};
char val[30];
int i,j,k,n,l;
clrscr();
printf("\n enter the number :");
scanf("%d",&n);
j=0;i=0;
for(;n>0;n=n/10)
{
k=n%10;
if((k==9)||(k==4))
{
if(k==9)
val[i++]=ch[j+2];
else
val[i++]=ch[j+1];
val[i++]=ch[j];
}
else if((k>=5)&&(k<=8))
{
for(;k!=5;k--)
val[i++]=ch[j];
val[i++]=ch[j+1];
}
else if((k>=1)&&(k<=3))
for(;k!=0;k--)
val[i++]=ch[j];
j=j+2;
}
printf("\n ROMANS ARE:\n");
for(i=i-1;i>=0;i--)
printf("%c",val[i]);
getch();
}
Department of Computers

60

//mergesorting
void print(int a[],int n)
{
int i;
printf("\n\t");
for(i=0;i<n;i++)
printf("%d",a[i]);
}
void mergesort(int a[],int n)
{
int aux[10];
int i,j,k,l1,l2,u1,u2,size,pass=1;
clrscr();
size=1;
while(size<n)
{
printf("\npass %d:",pass++);
l1=0;
k=0;
while(l1+size<n)
{
l2=l1+size;
u1=l2-1;
u2=(l2+size-1)<n?(l2+size-1):(n-1);
for(i=l1,j=l2;i<=u1&&j<=u2;k++)
{
if(a[i]<a[j])
aux[k]=a[i++];
else
aux[k]=a[j++];
}
for(;i<=u1;i++,k++)
aux[k]=a[i];
for(;j<u2;j++,k++)
aux[k]=a[j];
l1=u2+1;
}
Department of Computers

61

for(i=l1;k<n;i++,k++)
aux[k]=a[i];
for(i=0;i<n;i++)
a[i]=aux[i];
size*=2;
printf(a,n);
}

}
main()
{
int a[10],n,i;
printf("\n enter the limit :");
scanf("%d",&n);
printf("\n enter the numbers..\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
mergesort(a,n);
printf("\n the stored list is ..\n");
for(i=0;i<n;i++)
printf("%d",a[i]);
getch();
}

Department of Computers

62

//program to compute cosine series
#include<stdio.h>
#include<math.h>
void main()
{
float x,t,sum;
int i,n=20;
clrscr();
printf("input x\n");
scanf("%f",&x);
printf("%6.2f\n",x);
x=x*3.1412/180;
t=1;
sum=1;
for(i=1;i<n+1;i++)
{
t=t*pow((double)(-1),(double)(2*i-1))*x*x/(2*i*(2*i*1));
sum=sum+t;
}
printf("cos(x)=%7.3f\n",sum);
}

Department of Computers

63

//finding the second largest and smallest elements inthe
vector
#include<stdio.h>
void main()
{
int i,j,k,n,num;
float a[50],b[50],temp;
clrscr();
printf("size of vector?");
scanf("%d",&n);
printf("%d\n",n);
num=n;
printf("\n vector elements?\n");
for(i=0;i<n;i++)
{
scanf("%f",&a[i]);
b[i]=a[i];
}
for(i=0;i<n;i++)
printf("%8.2f",a[i]);
printf("\n");
//copying vector AtoB without duplicates
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
{
if(b[i]==b[j])
{
for(k=j;k<n-1;k++)
b[k]=b[k+1];
n=n-1;
j=j-1;
}
}
//sorting vector Bwithout duplicates
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
{
if(b[i]>b[j])
{
Department of Computers

64

temp=b[i];
b[i]=b[j];
b[j]=temp;
}
}
printf("\n vector A stored and without duplicates :\n");
for(i=0;i<n;i++)
printf("%8.2f",b[i]);
printf("\n");
printf("\n second largest element :%8.2f\n",b[n-2]);
printf("\nsecond smallest element :%8.2f\n",b[1]);
}

Department of Computers

65

//deleting dupliates in a vector
#include<stdio.h>
void main()
{
int i,j,k,n,num,flag=0;
float a[50];
clrscr();
printf(" size of vector ?");
scanf("%d",&n);
printf("%d\n",n);
num=n;
printf("\n vector elements ?\n");
for(i=0;i<n;i++)
scanf("%f",&a[i]);
for(i=0;i<n;i++)
printf("%6.2f",a[i]);
printf("\n");
//removeing duplicates
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
{
if(a[i]==a[j])
{
n=n-1;
for(k=j;k<n;k++)
a[k]=a[k+1];
flag=1;
j=j-1;
}
}
if(flag==0)
printf("\n no duplicates found in vector\n");
else
{
printf("\n vector has %d duplicates\n\n",num-n);
printf("vector after deleting duplicates :\n");
for(i=0;i<n;i++)
printf("%6.2f",a[i]);
printf("\n");
Department of Computers

66

}
}

Department of Computers

67

//to sort the matrix row-wise
#include<stdio.h>
void main()
{
int i,j,m,n,a[10][10],k,temp;
void sort(int a[][10],int,int,int);
clrscr();
printf("enter the size of matrix :\n");
scanf("%d%d",&m,&n);
printf("enter the matrix elements :\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
k=n;
sort(a,k,m,n);
}
void sort(a,k,m,n)
int a[10][10],k,m,n;
{
int flag,i,j,temp;
do
{
flag=1;
for(i=0;i<m;i++)
{
for(j=0;j<k;j++)
{
if(a[i][j]<a[i][j+1])
{
temp=a[i][j];
a[i][j]=a[i][j+1];
a[i][j+1]=temp;
flag=0;
}
}
}
k-=1;
Department of Computers

68

} while(flag==0);
printf("stored matrix \n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%8d",a[i][j]);
printf("\n");
}
}

Department of Computers

69

//fibonaccisequence generation by recursion
#include<stdio.h>
void main()
{
int n;
void fib();
clrscr();
printf("number of terms tobe generated?");
scanf("%d",&n);
printf("%d",n);
printf("\n\nfibonaccisequence upto %d terms :\n\n");
fib(n);
printf("\n");
}
void fib(n)
int n;
{
static int f1=0,f2=1;
int temp;
if(n<2)
{
f1=0;
f2=1;
}
else
{
fib(n-1);
temp=f2;
f2=f1+f2;
f1=temp;
}
printf("%5d",f1);
return;
}

Department of Computers

70

//binarysearch using recursion
#include<stdio.h>
#include<conio.h>
int key;
void main()
{
int a[50],i,n,loc;
int bin();
clrscr();
printf("enter the size of the array?");
scanf("%d",&n);
printf("\n array elements in (ascending order)?\n\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\n");
printf("\n element to the searched?");
scanf("%d",&key);
loc=bin(a,0,n);
if(loc==0)
else
{
printf("successful search\n");
printf("%d found at position %d\n",key,loc);
}
getch();
}
int bin(b,low,high)
int b[50],low,high;
{
static int mid;
int i;
if(low<=high)
{
mid=(low+high)/2;
if(key<b[mid])
{
high=mid-1;
bin(b,low,high);
}
else
if(key>b[mid])
{

Department of Computers

71
low=mid+1;
bin(b,low,high);
}
else if(key==b[mid])
return(mid+1);
}
else
return(0);
}

Department of Computers

72

//replace a portion of string
#include<stdio.h>
void main()
{
char mainstr[50],repstr[50],save[50];
int i,j,k,pos,num,last,l1,l2,l;
printf("enter the main string:");
scanf("%[^\n]",mainstr);
for(l1=0;mainstr[l1]!='\0';l1++);
printf("\n len is %d\n",l1);
printf("\n position from where it it to be replaced?");
scanf("%d",&pos);
printf("\n\n number of char to be replaced?");
scanf("%d",&num);
printf("\n\n enter the replacement string :");
scanf("%s",repstr);
for(l2=0;repstr[l2]!='\0';l2++)
printf("\nlen is %d\n",l2);
//if position exceeds string length
//append replace string at the end
if(pos>l1)
{
for(i=0;i<l2;i++)
mainstr[l1+i-1]=repstr[i];
l=l1+l2;
}
else
{
last=pos+l2-1;
j=0;
for(i=pos+num-1;i<l1;i++)
{
//saveing the end portion of the string
save[j]=mainstr[i];
j++;
}
l=j;
j=0;
//replaceing string
for(i=--pos;i<last;i++)
{
mainstr[i]=repstr[j];
j++;

Department of Computers

73
}
//copying back th saved string
for(i=0;i<l;i++)
{
mainstr[last]=save[i];
last++;
}