C Programs
C Programs
Programming in C
NAME
ROLL NO
REGISTER NO :
BONAFIED CERTIFICATE
DEPARTMENT OF COMPUTER SCIENCE
is
to
certify
that
the
bonafide
record
work
done
by
Lecturer-in-charge
Internal examiner
External examiner
DATE
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
TITLE
SUMMATION OF SERIES
Sin(x)
Cos(x)
Exp(x)
String Manipulation
Vowels, Consonants, Words, Spaces
Counting
Reverse a string and Palindrome
Replace substring
Substring Detection and count removal
Recursion
NCR
NPR
GCD
Factorial
Fibonacci Series
Towers of Hanoi
Matrix Manipulations
Matrix Addition
Matrix Subtraction
Matrix Multiplication
Transpose Matrix
Determinant of Matrix
Sorting
Insertion Sort
Bubble Sort
Selection sort
Searching
Linear Search
Binary Search
Sin(X)
#include<stdio.h>
PAGE
NO
SIGN
#include<math.h>
#define PI 3.143
main()
{
int n,x,f1,f2,i,j;
double sin2;
float t,s1,s2,sin1;
clrscr();
printf("\n\t\t\t COMPUTATION OF SIN(X)");
printf("\n\t\t\t .....................");
printf("\n\n Enter the number of terms:");
scanf("%d,&n);
printf("\n\n Enter the degree value:");
scanf"%d",&x);
s1=2=0;
t=x*PI\180;
for(i=1;i<=n;i+=4)
{
f1=1;
for(j=1;j<=I;j++)
{
f1=f1*j;
}
s1=s1+(pow(t,i)/f);
}
for(i=3;i<=n,i+=4)
{
f2=1;
for(j=1;j<=I;j++)
{
f2=f2*j;
}
s2=s2+pow(t,i/f2)
}
sin1=s1-s2;
sin2=sin(t);
printf("\n\n The computed value of sin(%d) is%8.3f",x,sin1);
printf("\n\n The built-in sine value of%d id%8.3f",x,sin2);
getch();
}
OUTPUT
COMPUTATION OF SIN(X)
Enter the number of terms : 10
Enter the degree value : 45
The computated value of sin(45) is 0.707
The built-in sine value of 45 is 0.707
Cos(X)
#include<stdio.h>
#include<math.h>
#define PI 3.143
main()
{
int n,f1,f2,i,j;
double cos2;
float t,x,s1,s2,cos1;
clrscr();
printf("\n\t\t\t COMPUTATION OF COS(X));
printf("\n\t\t\t .....................");
printf("\n\n Enter the number of terms:");
scanf("%d,&n);
printf("\n\n Enter the degree value:");
scanf(%f, &x);
s1= s2=0;
t=x*PI\180;
for(i=4;i<=n;i+=4)
{
f1=1;
for(j=1;j<= i;j++)
{
f1=f1*j;
}
s1=s1+(pow(t,i)/f1);
}
for(i=2;i<=n,i+=4)
{
f2=1;
for(j=1;j<= i;j++)
{
f2=f2*j;
}
s2=s2+(pow(t,i)/f2);
}
cos1=1+s1-s2;
cos2 = cos(t);
printf("\n\n The computed value of cos(%5.2f) is%8.3f",x, cos1);
printf("\n\n The built-in cosine value of%5.2f is %8.3f",x, cos 2);
getch();
}
OUTPUT
COMPUTATION OF cos(x)
Enter the number of terms : 10
Enter the degree value : 45
The computated value of cos(45) is 0.707
The built-in cosine value of 45 is 0.707
Exp(X)
#include<stdio.h>
#include<math.h>
#define PI 3.143
main()
{
int i,j,k,n;
float expn,x,f,sum,p;
clrscr();
printf("\n\t\t\t EXPONENT SERIES);
printf("\n\t\t\t .....................");
printf("\n\n Enter the number of terms:");
scanf("%d,&n);
printf("\n\n Enter the value of x:");
scanf(%f, &x);
sum=1;
for(i=1;i<=n;i++)
{
p=pow(x,i);
f1=1;
for(j=1;j<= i;j++)
f1=f*j;
sum=sum+(p/f);
}
expn=exp(x);
printf("\n\n The computed value of exp(%f) is%8.4f",x, sum);
printf("\n\n The exponent value of%6.2f is %8.4f",x, expn);
getch();
}
OUTPUT
EXPONENT SERIES
Enter the number of terms : 8
Enter the value of x : 2
The computated value of exp(2.000000) is 7.3873
The exponent value of 2 is 7.3891
/* VOWELS */
#include<stdio.h>
#include<string.h>
main()
{
char str[80],ch;
int i,c=0,s=0,v=0,w=1,len;
clrscr();
printf("\n\tNo. of vowels, consonants, spaces and words");
printf("\n\t-------------------------------------------");
printf("\n\n\nEnter the String:\n");
gets(str);
len=strlen(str);
for(i=0;i<len;i++)
{
ch=str[i];
switch(toupper(ch))
{
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
{
v++;
break;
}
case ' ':
{
s++;
break;
}
default:
{
c++;
break;
}
}
if((str[i]==' ')&&(str[i-1] != ' '))
w++;
}
printf("\n\n Entered string is %s",str);
printf("\n\n No. of Vowels:%d",v);
printf("\n\n No. of Consonants:%d",c);
printf("\n\n No. of Spaces:%d",s);
printf("\n\n No. of Words:%d",w);
getch();
}
OUTPUT SCREENSHOT
/* Palindrome */
#include<stdio.h>
#include<string.h>
#define size 26
void main()
{
char strsrc[size];
char strtmp[size];
clrscr();
printf("\n Enter String:= "); gets(strsrc);
strcpy(strtmp,strsrc);
strrev(strtmp);
if(strcmp(strsrc,strtmp)==0)
printf("\n Entered string \"%s\" ispalindrome",strsrc);
else
printf("\n Entered string \"%s\" is not
palindrome",strsrc);
getch();
}
OUTPUT SCREENSHOT
#include<string.h>
Main()
{
Char str[80],substr[80],restr[80],newstr[80];
Int I,j,n,k=0,1,slen,sublen,replen,pos[80]={0},count=flag=1;
Clrscr();
Printf(\n \t\t\t\t FIND AND REPLACE A SUBSTRING);
Printf(\n \t\t\t\t);
Printf(\n \n enter the string\n);
Gets(str);
Printf(\n \n enter the substring to be searched\n);
Gets(substr);
Printf(\n \n enter the substring to be replace\n);
Gets(repstr);
Slen=strlen(str);
Sublen=strlen(substr);
Replen=strlen(repstr);
For(i=0;i<slen-sublen+1;i++)
{
Flag=1;
For(j=0,n=0;n<sublen;j++)
{
If(str[j]!=substr[n])
Flage=0;
}
If(flage==1)
{
Pos[k]=1;
K++;
Count++;
}
}
K=0;
If(count>0)
{
For(i=0,j=0;i<slen;i++)
{
If(i!=pos[k])
{
Newstr[i]=str[i];
J++;
Else
{
for(1=0;1<replen;1++)
{
Newstr[i]=replen[1];
J++;
If((replan<sublen&&(slen<=pos[k]+sublen))
{
Newstr[i]=\0.
Printf(\n \n the string after replace is:%s,newstr);
}
Else
Printf(\n \n the substring could not be found.);
getch();
}
OUTPUT
FIND AND REPLACE A STRING
Enter the string
Is it my book?
Enter the substring to be searched
My
Enter the substring to be replaced
Yours truly
The string after replace is : is it your book?
main()
{
char str[80],substr[80];
int i,j,k=0,slen,sublen,pos[80],count=0,flag=1;
clrscr();
printf(\n\t\t\t\FIND A SUBSTRING);
printf(\n\t\t\t\t...);
printf(\n\nEnter the string\n);
gets(str);
printf(\n\nEnter the substring\n);
gets(substr);
slen=strlen(str);
sublen=strlen(substr);
for(i=0;i<slen-sublen+1;i++)
{
flag=1
for(j=i;n=0;n<sublen;j++,n++)
{
if(str[j]!=substr[n])
flag=0;
}
if(flag==1)
{
clrscr();
pos[k]=i+1;
k++;
count++;
}
}
if(count>0)
{
printf(\n\n%soccurs%d times in %s\n,substr,count,str);
for(k=0;k<count;k++)
Printf(\n%soccurs at position# %d in %s,substr,pos[k],str);
else
Printf(\n\n%s is not found in %s,substr,str);
getch();
}
OUTPUT
FIND A SUBSTRING
Enter the string
This is my book
Enter the substring is
is occurs 2 times in This is my book
is occurs at position# 3 in This is my book
is occurs at position# 6 in This is my book
/* NCR */
#include <stdio.h>
#include <conio.h>
long double fact( int f)
{
if (f==0)
return (1);
else
{
return(f*fact(f-1));
}
}
int ncr ( int n, int r)
{
return( fact( n) / (fact( r) * fact(n- r) ) ) ;
}
void main()
{
int n , r, ncr( int , int);
long double fact( int);
clrscr();
printf(" enter value of n & r \n");
scanf("%d %d",&n , &r);
if( n>= r)
printf(" ncr is %d\n", ncr( n , r));
else
printf(" n cannot be less than r ie., n >= r");
getch();
}
OUTPUT SCREENSHOT
/* NPR */
#include <stdio.h>
#include <conio.h>
long double fact( int f)
{
if (f==0)
return (1);
else
{
return(f*fact(f-1));
}
}
long npr( int n , int r)
{
return( fact( n) / fact( n- r)); /* return npr result*/
}
main()
{
int n , r;
long npr( int , int);
long double fact( int);
clrscr();
printf(" enter value of n & r \n");
scanf("%d %d",&n , &r);
if( n>= r)
{
printf(" npr is %ld", npr( n, r));
}
else
printf(" n cannot be less than r ie., n >= r");
getch();
}
OUTPUT SCREENSHOT
/* GCD */
#include <stdio.h>
#include <conio.h>
int gcd( int m, int n)
{
while( m!= n) /* execute loop until m == n*/
{
if( m > n)
m= m - n; /* large - small , store the results in large variable*/
else
n= n - m;
}
return ( m); /* m or n is GCD*/
}
void main()
{
int m, n , gcd( int ,int);
printf(" enter anyu 2 values \n");
scanf("%d%d", &m,&n);
printf(" gcd is %d\n", gcd( m, n));
getch();
}
OUTPUT SCREENSHOT
/* FACTORIAL */
#include<stdio.h>
long int factorial(int n)
main()
{
Int n;
Long int factorial(int n);
Clrscr();
Printf(enter the number of n:);
Scanf(%d,&n);
Printf(the value n!=%\d,factorial(n));
Getch();
}
Long int factorial(int n)
{
If(n<=1)
Return(1);
Else
Return(n*factorial(n=1));
}
OUT PUT
The value N!
120
/* Fibonacci Series */
#include <stdio.h>
#include <conio.h>
void fib(int, int, int);
void main()
{
int n , old=-1,new=1;
clrscr();
printf(" enter value of n fib to print");
scanf("%d",&n);
fib(old,new,n);
getch();
}
void fib(int old,int new,int n)
{
int temp;
static int count;
count++;
if(count <= n)
{
temp=old+new;
printf(" %d ",temp);
fib(new,temp,n);
}
}
OUTPUT SCREENSHOT
/* TOWER OF HANOI */
#include <conio.h>
#include <stdio.h>
void main()
{
void hanoi(char,char,char,int);
char t1='A',t2='B',t3='C';
int n;
clrscr();
printf("\n Enter the no. of disks on Tower A:");
scanf("%d",&n);
if(n<1)
{
printf("\n Nothing to move");
}
else
hanoi(t1,t2,t3,n);
getch();
}
void hanoi(char t1,char t2,char t3,int n)
{
static int step=0;
step++;
printf("\n %c %c %c %d",t1,t2,t3,n);
if(n==1)
{
printf("\n Move top disk from Tower %c ----> %c",t1,t2);
return;
}
hanoi(t1,t3,t2,n-1);
printf("\n %c %c %c %d",t1,t2,t3,n);
printf("\n Move top disk from Tower %c ----> %c",t1,t2);
printf("\n %c %c %c %d",t1,t2,t3,n);
hanoi(t3,t2,t1,n-1);
printf("\n %c %c %c %d steps=%d",t1,t2,t3,n,step);
}
OUTPUT SCREENSHOT
void main()
{
int i,j,c1,c2,r1,r2;
int m1[10][10],m2[10][10],m3[10][10];
clrscr();
printf("\n Enter the number of row and coloum for matrix 1:-");
scanf("%d%d",&r1,&c1);
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
scanf("%d",&m1[i][j]);
}
}
printf("\n Enter the number of row and coloum for matrix 2:-");
scanf("%d%d",&r2,&c2);
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
scanf("%d",&m2[i][j]);
}
}
if((r1==r2)&&(c1==c2))
{
printf("\n Addition is possible:-");
for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
m3[i][j]=m1[i][j] + m2[i][j];
printf("\n addition of m1&m2 = :-");
for(i=0;i<r1;i++)
{
printf("\n");
for(j=0;j<c1;j++)
printf("%d ",m3[i][j]);
}
}
else
printf("\n Addition is not possible r1 != r2 or c1 != c2 ");
getch();
}
OUTPUT SCREENSHOT
void main()
{
int i,j,c1,c2,r1,r2;
int m1[10][10],m2[10][10],m3[10][10];
printf("\n Enter the number of row and coloum for matrix 1:-");
scanf("%d%d",&r1,&c1);
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
scanf("%d",&m1[i][j]);
}
}
printf("\n Enter the number of row and coloum for matrix 2:-");
scanf("%d%d",&r2,&c2);
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
scanf("%d",&m2[i][j]);
}
}
if((r1==r2)&&(c1==c2))
{
printf("\n subtraction is possible:-");
for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
m3[i][j]=m1[i][j] - m2[i][j];
printf("\n sub. of m1&m2 = :-");
for(i=0;i<r1;i++)
{
printf("\n");
for(j=0;j<c1;j++)
printf("%d ",m3[i][j]);
}
}
else
printf("\n subtraction is not possible r1 != r2 or c1 != c2 ");
getch();
}
OUTPUT SCREENSHOT
void main()
{
int i,k,j,c1,c2,r1,r2;
int m1[10][10],m2[10][10],m3[10][10];
clrscr();
printf("\n Enter the number of row and coloum for matrix 1:-");
scanf("%d%d",&r1,&c1);
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
scanf("%d",&m1[i][j]);
}
}
printf("\n Enter the number of row and coloum for matrix 2:-");
scanf("%d%d",&r2,&c2);
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
scanf("%d",&m2[i][j]);
}
if(c1==r2)
{
printf("\n Multiplication is possible:-");
for(i=0;i<r1;i++)
for(j=0;j<c2;j++)
{
m3[i][j]=0;
for(k=0;k<c1;k++)
m3[i][j]=m3[i][j]+m1[i][k]*m2[k][j];
}
printf("\n Multiplication of Matrix:-\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
printf("\t%d",m3[i][j]);
printf("\n");
}
}
else
printf("\n Multiplication is not possible as c1 != r2 ");
getch();
}
OUTPUT SCREENSHOT
/* TRANSPOSE MATRIX */
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,c1,r1;
int m1[10][10],m2[10][10];
clrscr();
printf("\n Enter the number of row and coloum for matrix 1:-");
scanf("%d%d",&r1,&c1);
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
printf("\n enter the value for m1[%d][%d] = ",i,j);
scanf("%d",&m1[i][j]);
m2[j][i]=m1[i][j];
}
}
for(i=0;i<c1;i++)
for(j=0;j<r1;j++)
m1[i][j]=m2[i][j];
printf("\n transpose of matrix = :-");
for(i=0;i<c1;i++)
{
printf("\n");
for(j=0;j<r1;j++)
printf("%d ",m1[i][j]);
}
getch();
}
OUTPUT SCREENSHOT
/* DETERMINATE OF A MATRIX */
#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
float arr[10][10]={0},r,d=1;
int a[10][10],I,j,k=1,1=0,n;
Clrscr();
Printf(\n\t\t\t\t DETERMINATE OF A MATRIX);
Printf(\n\t\t\t\t ****************************);
Printf (\n\n enter the size of the matrix:);
Scanf(%d,&n);
For(i=0;i<n;i++)
For(j=0;j<n;j++)
{
Printf(\n enter the value for a[%d][%d]:,I,j);
Scanf(%d,&a[i][j]);
Arr[i][j]=a[i][j];
}
While(k<n)
{
For(i=k;i<n;i++)
{
If(arr[1][1]==0)
{
Printf(\n\n singular matrix determinate =0);
Exit(0);
}
R=(arr[i][1]/arr[1][1]);
For(j=k;j<n;j++)
Arr[i][j]=arr[i][j]-arr[1][j]*r;
}
1=k;
K++;
}
For(i=0;i<n;i++)
D=d*arr[i][i];
If(d==0)
Printf(\n\n singular matrix-determinate =0);
Else
Printf(\n\n the value of determinate is%5.2f ,d);
getch();
}
OUT PUT
DETERMINATE OF A MATRIX
Enter the size of the matrix
-2.00
INSERTION SORT
#include<stdio.h>
main()
{
int a[100];
int i,temp,loc,n;
Clrscr();
Printf(\n\t\t\t\t INSERTION SORT);
Printf(\n\t\t\t\t ------------------------);
Printf(\n\n ENTER THE NUMBER OF TERMS:);
Scanf(%d,&n);
For(i=1;i<=n;i++);
{
Printf(\n ENTER THE VALUE FOR ELEMENT#%d:,i);
Scanf(%d,&a[i]);
}
a[0]=-32767;
printf(\n\t\t\t\t ORIGINAL MATRIX\N\N);
for(i=1;i<=n;i++);
printf(%d\t,a[i]);
for(i=2;i<=n;i++)
{
temp=a[i];
loc=i-1;
while(temp<a[loc]);
{
a[loc+1]=a[loc];
loc=loc-1;
}
a[loc+1]=temp;
}
Printf(\n\t\t\t\t Sorted matrix\n\n);
for(i=1;i<=n;i++);
printf(%d\t.a[i]);
getch();
}
OUTPUT
INSERTION SORT
Enter the number of terms
-2
-11
-9
22
ORIGINAL MATRIX
-2 0 4 -11 -9
22
SORTED MATRIX
-11 -9
-2
0 1 3 4
/* BUBBLE SORT */
22
#include<stdio.h>
main()
{
int a[100],i,j,n,temp;
clrscr();
printf("\n\n\t\t\t\tBUBBLE SORT");
printf("\n\t\t\t\t----------------");
printf("\n\nENTER THE NUMBER OF TERMS:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nENTER THE VALUE OF ELEMENT#%d:",i+1);
scanf("%d",&a[i]);
}
clrscr();
printf("\n\t\t\t\tTHE ORGINAL ELEMENTS ARE\n");
for(i=0;i<n;i++)
printf("\n\t\t\t\t%d",a[i]);
for(i=0;i<n-1;i++)
{
for(j=i,j<n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("\n\n\t\t\tTHE SORTED ELEMENTS ARE\n");
for(i=0;i<n;i++)
printf("\n\t\t\t\t%d",a[i]);
getch();
}
OUTPUT
-2
-4
-6
-8
SELECTION SORT
-10
#include<stdio.h>
main()
{
int a[10];
int I,j=0,min=32676,temp,loc,n;
clrscr();
printf(\n\t\t\t\t SELECTION SORT);
printf(\n\t\t\t\t ********** *****);
printf(\n\n enter the number of terms :);
scanf(%d,&n);
for(i=0;i<n;i++)
{
Printf(\n enter the value for element#%d:,i+1);
Scanf (%d,&a[i]);
If (min >a[i])
{
Min=a[i];
Loc=I;
}
}
Printf(\n\t\t\t\t original matrix\n\n);
For(i=0;i<n;i++)
{
Temp=a[loc];
A[loc]=a[i];
A[i]=temp;
Min=a[i+1];
For)j=i+1;j<n;j++)
{
If(min>a[j])
{
Min=a[j];
Loc=j;
}
}
}
Printf(\n\t\t\t\t sorted matrix\n\n);
For(i=0;i<n;i++)
Printf(%d\t,a[i]);
getch();
}
OUTPUT
SELECTION SORT
Enter the number of terms
-2
-11
-9
22
ORIGINAL MATRIX
-2 0 4 -11 -9 22 3 1
SORTED MATRIX
-11 -9 -2 0 1 3 4 22
LINEAR SEARCH
#include<stdio.h>
#include<string.h>
Main()
{
Static char names[50][10],findname[10];
Int I,n,flag=0,pos;
Clrscr();
Printf(\n\t\t\t\t LINEAR SEARCH);
Printf(\n\t\t\t\t ------------------------);
Printf(\n\n Enter the number of string);
Scanf(%d,&n);
For(i=0;i<n;i++)
{
Printf(\n Enter name#%d:,i+1);
Scanf(%s,names[i]);
}
Clrscr();
Printf(\n Enter the name to be searched:);
Scanf(%s,findname);
For(i=0;i<n;i++);
{
If(strcmp(names[i],findname)==0)
{
Flag=1;
Pos=i+1;
break;
{
}
If(flag)
Printf(\n\n%s is found at position%d,findname,pos);
Else
Printf(\n\n%s is not found in the list,findname);
getch();
}
OUTPUT
LINEAR SEARCH
Enter the number of strings
10
Enter name#1
raja
Enter name#2
kannan
Enter name#3
mani
Enter name#4
sarath
Enter name#5
manish
Enter name#6
adhitya
Enter name#7
resmi
Enter name#8
suganya
Enter name#9
lakshmi
Enter name#10
francis
sarath
/* BINARY SEARCH */
#include<stdio.h>
#include<string.h>
main()
{
static char names[50][10], findname[10], *temp;
int i,j,n,flag=0, first, last, middle;
clrscr();
printf(\n\t\t\t\tBINARY SEARCH);
printf(\n\t\t\t\t_________________);
printf(\n\nEnter the number of strings:);
scanf(%d,&n);
for(i=0;i<n;i++)
{
Printf(\nEnter name#%d:,i+1);
Scanf(%s,names[i]);
}
Clrscr();
Printf(\n\nEnter the names to be searched:);
Scanf(%s,findname);
For(i=0;i<n-1;i++)
For(j=I;j<n;j++)
If(strcmpi(names[i],names[j]>0)
{
Strcpy(temp,names[j]);
Strcpy(names[j],names[i]);
Strcpy(names[i],temp);
}
First=0;
Last=n;
Do
{
Middle=(first+last)/2;
If(first>las)
Break;
If(strcmpi(names[middle],findname)<0)
First=middle+1;
Else
If(strcmpi(names[middle],findname)>0)
Last=middle-1;
Else
{
Flag=1;
Break;
}
}
while(1);
if(flag)
OUTPUT
BINARY SEARCH
Enter the number of strings
10
Enter name# 1
Raja
Enter name# 2
Kannan
Enter name# 3
Mani
Enter name# 4
Sharath
Enter name# 5
Manish
Enter name# 6
Aditya
Enter name# 7
Resmi
Enter name# 8
Suganya
Enter name# 9
Lakshmi
Enter name# 10
Francis
Sharath