You are on page 1of 50

INDEX

S.NO

week
1

DESCRIPTION OF THE PROGRAM

a) A C program to find the sum of individual digits of a positive integer.


b) A Fibonacci Sequence is defined as follows: the first and second terms in the
sequence are 0 and 1. Subsequent terms are found by adding the preceding two
terms in the sequence.
c) Write a C program to generate all the prime numbers between 1 and n. Where n
is the value supplied by the user.

week
2

a) A C program to calculate the following Sum:


Sum=1-x2/2! +x4/4!-x6/6!+x8/8!-x10/10!
b) A C program toe find the roots of a quadratic equation.

week
3

i) C programs that use both recursive and non-recursive functions to find the
factorial of a given integer.
ii) C programs that use both recursive and non-recursive functions to find the GCD
of two given integers.
iii) C programs that use both recursive and non-recursive functions to sovle
towers of Hanoi problem.

week
4

A)Program to find the total distance travelled by vehicle in 't' seconds is given by
distance = ut+1/2at2 where 'u' and 'a' are the initial velocity (m/sec.) and acceleration
(m/sec2) for
a) different values of 'u' and 'a'.
b) A C program, which takes two integer operands and one operator form the user,
performs the operation and then prints the result.
(Consider the operators +,-,*,/,% and use Switch Statement)

week
5

a) C program to find both the largest and smallest number in a


list of integers.
b) A C program that uses functions to perform the following:
i) Addition of Two Matrices
ii) Multiplication of Two Matrices

week
6

a) A C program that uses functions to perform the following operations:


i) To insert a sub-string in to given main string from a given
b) A C program to determine if the given string is a palindrome or not.

JOGINPALLY B.R.ENGINEERING COLLEGE

Page 1

week
7

a) A C program that displays the position or index in the string S


where the string T begins, or - 1 if S doesn't contain T.

week
8

b) A C program to count the lines, words and characters in a given text.


a) A C program to generate Pascal's triangle.
b) A C program to construct a pyramid of numbers.

week
9

A C program to read in two numbers, x and n, and then compute the sum of this
geometric progression: 1+x+x2+x3+.+xn
For example: if n is 3 and x is 5, then the program computes 1+5+25+125.
Print x, n, the sum Perform error checking. For example, the formula does not make
sense for negative exponents - if n is less than 0. Have your program print an error
message if n<0,then go back and read in the next pair of numbers of without
computing the sum. Are any values of x also illegal ? If so, test for them too.

week
10

a) A C program to find the 2s complement of a binary number.


b) A C program to convert a Roman numeral to its decimal equivalent.

week
11

A C program that uses functions to perform the following


operations:
i) Reading a complex number
ii) Writing a complex number
iii) Addition of two complex numbers
iv) Multiplication of two complex numbers
(Note: represent complex number using a structure.)

week
12

a) A C program which copies one file to another.


b) A C program to reverse the first n characters in a file.
(Note: The file name and n are specified on the command line.)

week
13

A C program that uses functions to perform the following operations on Single


Linked list:
i)
Creation
ii) Insertion
ii)
Deletion
iv) Traversal

week
14

A C program that uses functions to perform the following operations on doubly


linked list:
i)Creation ii) Insertion
iii)Deletion iv) Traversal in both ways
i)C programs that implement stack (its operations) using Arrays

JOGINPALLY B.R.ENGINEERING COLLEGE

Page 2

week
15
week
16

ii) C programs that implement stack (its operations) using pointers


i) C programs that implement Queue (its operations) using Arrays.
ii) C programs that implement Queue (its operations) using pointers.

week
17

A C program that uses Stack operations to perform the following:


i) Converting infix expression into postfix expression
ii) Evaluating the postfix expression

week
18

week
19

week
20

A C program that uses functions to perform the following:


i) Creating a Binary Tree of integers
ii)Traversing the above binary tree in preorder, inorder
and postorder.
i)

C programs that use both recursive and non recursive functions to


perform Linear search operations for a Key value in a given list of
integers.

ii)

C programs that use both recursive and non recursive functions to


perform binary search operations for a Key value in a given list of
integers.

A C program that implements the following sorting methods to sort a given list of
integers in ascending order
i) Bubble Sort
ii) Quick Sort

week
21

A C program that implements the following sorting methods to sort a given list of
integers in ascending order
i) insertion Sort
ii)Merge sort Sort

week
22

i) A C program to implement the Lagrange interpolation


ii) A C program to implement the Newton-Gregory forward interpolation.

week
23

i) A C program to implement the Linear Regression algorithm.


ii) A C program to implement the polynomial regression algoritm.

week
24

i) A C program to implement trapezoidal method.


ii) A C program to implement Simpson method.

JOGINPALLY B.R.ENGINEERING COLLEGE

Page 3

Sum of individual digits of a given positive integer


Program to find the sum of the individual digits of a given positive integer
#include<stdio.h>
#include<conio.h>
void main()
{
int no,sum,dig,t;
clrscr();
printf("\nEnter a positive integer:");
scanf("%d",&no);
t=no;
sum=0;
dig=0;
while(no>0)
{
dig=no%10;
sum=sum+dig;
no=no/10;
}
printf("\nm of the digits of the given positive integer %d is %d",t,sum);
getch();
}

Output:
INPUT:
Enter a positive integer : 1234
RESULT:
Sum of the digits of the given positive integer 1234 is 10

JOGINPALLY B.R.ENGINEERING COLLEGE

Page 4

Fibonacci sequence
Program to generate Fibbonacci Series
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,i,n;
clrscr();
printf("\nEnter how many terms:");
scanf("%d",&n);
a=0;
b=1;
printf("\n%d\n%d",a,b);
i=3;
while(i<=n)
{
c=a+b;
printf("\n%d",c);
a=b;
b=c;
i++;
}
getch();
}

OUTPUT
INPUT :
Enter how many terms : 10
RESULT :
0
1
1
2
3
5
8
13
21
34

JOGINPALLY B.R.ENGINEERING COLLEGE

Page 5

Prime Numbers

Program to generate prime numbers between 1 and n


#include<stdio.h>
#include<conio.h>
void main()
{
int no,i,j,nof;
clrscr();
printf("\nEnter range 1 to ___:");
scanf("%d",&no);
for(i=1;i<=no;i++)
{
nof=0;
for(j=1;j<=i;j++)
{
if(i%j==0)
nof++;
}
if(nof==2)
printf("%d ",i);
}
getch();
}

OUTPUT
INPUT :
Enter range 1 to 20
RESULT:
2 3 7 11 13 17 19

JOGINPALLY B.R.ENGINEERING COLLEGE

Page 6

To evaluate the series


sum = 1 - x^2/2! + x^4/4! - x^6/6! + x^8/8! - x^10/10!
Program to solve sum = 1 - x^2/2! + x^4/4! - x^6/6! + x^8/8! - x^10/10!
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,x,n;
long num,den;
float sum;
clrscr();
printf("\nEnter value of x : ");
scanf("%d",&x);
sum=1;
num=1;
den=1;
for(j=2,i=1;j<=10;i++,j=j+2)
{
num=-num*x*x;
den=den*(2*i-1)*(2*i);
sum=sum+(float)num/den;
}
printf("\nSum=%f",sum);
getch();
}

OUTPUT
INPUT :Enter value of x : 2
RESULT :Sum=-0.416155

JOGINPALLY B.R.ENGINEERING COLLEGE

Page 7

To find the Roots of a quadratic equation


Program to find the roots of a quadratic equation
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int a,b,c,d;
float r1,r2;
clrscr();
printf("Enter values of a,b,c:");
scanf("%d%d%d",&a,&b,&c);
d=b*b-4*a*c;
if(d>0)
{
r1=-(b+sqrt(d))/(2*a);
r2=-(b-sqrt(d))/(2*a);
printf("Roots are real and different\n%f %f",r1,r2);
}
else if(d==0)
{
r1=-b/(2*a);
r2=-b/(2*a);
printf("Roots are real and equal\n%f %f",r1,r2);
}
else
{
r1=-b/(2*a);
r2=sqrt(-d)/(2*a);
printf("Roots are imaginary \n%f+i%f",r1,r2);
}
getch();
}

OUTPUT
INPUT :
Enter values of a,b,c:1 2 1
RESULT:
Roots are real and equal
-1.000000 -1.000000
JOGINPALLY B.R.ENGINEERING COLLEGE

Page 8

To find the factorial of a given integer using


recursive and non recursive functions
Program to find the factorial of a given integer using Non-recursive and recursive functions
#include<stdio.h>
#include<conio.h>
unsigned int recr_factorial(int n);
unsigned int iter_factorial(int n);
void main()
{
int n,i;
long fact;
clrscr();
printf("Enter the number: ");
scanf("%d",&n);
if(n==0)
printf("Factorial of 0 is 1\n");
else
{
printf("Factorial of %d Using Recursive Function is %d\n",
n,recr_factorial(n));
printf("Factorial of %d Using Non-Recursive Function is
%d\n",n,iter_factorial(n));
}
getch();
}
/* Recursive Function*/
unsigned int recr_factorial(int n)
{
return n>=1 ? n * recr_factorial(n-1) : 1;
}
/* Non-Recursive Function*/
unsigned int iter_factorial(int n)
{
int fact = 1;
int i;
for(i = 1; i <= n; i++) {
JOGINPALLY B.R.ENGINEERING COLLEGE

Page 9

fact *= i;
}
return fact;
}
OUTPUT
INPUT:
Enter the number: 7
OUTPUT :
Factorial of 7 Using Recursive Function is 5040
Factorial of 7 Using Non-Recursive Function is 5040

JOGINPALLY B.R.ENGINEERING COLLEGE

Page 10

To find GCD of two given integers


Program to find the GCD of two given integers using Non-Recursive and Recursive functions
unsigned int GcdRecursive(unsigned m, unsigned n);
unsigned int GcdNonRecursive(unsigned p,unsigned q);
int main(void)
{
int a,b;
clrscr();
printf("Enter the two numbers whose GCD is to be found: ");
scanf("%d%d",&a,&b);
printf("GCD of %d and %d Using Recursive Function is %d\n", a,b,GcdRecursive(a,b));
printf("GCD of %d and %d Using Non-Recursive Function is %d\n",a,b,GcdNonRecursive(a,b));
getch();
return 0;
}

/* Recursive Function*/
unsigned int GcdRecursive(unsigned m, unsigned n)
{
if(n>m)
return GcdRecursive(n,m);
if(n==0)
return m;
else
return GcdRecursive(n,m%n);
}
/* Non-Recursive Function*/
unsigned int GcdNonRecursive(unsigned p,unsigned q)
{
unsigned remainder;
remainder = p-(p/q*q);
if(remainder==0)
return q;
else
while(remainder>0)
{
p=q;
q=remainder;
remainder=p%q;
JOGINPALLY B.R.ENGINEERING COLLEGE

Page 11

}
return q;
}

OUTPUT
INPUT :
Enter the two numbers whose GCD is to be found: 15 25
RESULT:
GCD of 15 and 25 Using Recursive Function is 5
GCD of 15 and 25 Using Non-Recursive Function is 5

Towers of Hanoi
Program to solve Towers of Hanoi problem using Non-Recursive and Recursive functions
#include<conio.h>
#include<stdio.h>
/* Non-Recursive Function*/
void hanoiNonRecursion(int num,char sndl,char indl,char dndl)
{
char stkn[100],stksndl[100],stkindl[100],stkdndl[100],stkadd[100],temp;
int top,add;
top=NULL;
one:
if(num==1)
{
printf("\nMove top disk from needle %c to needle %c ",sndl,dndl);
goto four;
}
two:
top=top+1;
stkn[top]=num;
stksndl[top]=sndl;
stkindl[top]=indl;
stkdndl[top]=dndl;
stkadd[top]=3;
JOGINPALLY B.R.ENGINEERING COLLEGE

Page 12

num=num-1;
sndl=sndl;
temp=indl;
indl=dndl;
dndl=temp;
goto one;
three:
printf("\nMove top disk from needle %c to needle %c ",sndl,dndl);
top=top+1;
stkn[top]=num;
stksndl[top]=sndl;
stkindl[top]=indl;
stkdndl[top]=dndl;
stkadd[top]=5;
num=num-1;
temp=sndl;
sndl=indl;
indl=temp;
dndl=dndl;
goto one;
four:
if(top==NULL)
return;
num=stkn[top];
sndl=stksndl[top];
indl=stkindl[top];
dndl=stkdndl[top];
add=stkadd[top];
top=top-1;
if(add==3)
goto three;
else if(add==5)
goto four;
}

/* Recursive Function*/
void hanoiRecursion( int num, char ndl1, char ndl2, char ndl3)
{
if ( num == 1 )
{
printf( "Move top disk from needle %c to needle %c.", ndl1, ndl2 );
return;
}
JOGINPALLY B.R.ENGINEERING COLLEGE

Page 13

hanoiRecursion( num - 1,ndl1, ndl3, ndl2 );


printf( "Move top disk from needle %c to needle %c.", ndl1, ndl2 );
hanoiRecursion( num - 1,ndl3, ndl2, ndl1 );
}
void main()
{
int no;
clrscr();
printf("Enter the no. of disks to be transferred: ");
scanf("%d",&no);
if(no<1)
printf("\nThere's nothing to move.");
else
printf("Non-Recursive");
hanoiNonRecursion(no,'A','B','C');
printf("\nRecursive");
hanoiRecursion(no,'A','B','C');
getch();
}
OUTPUT

INPUT :
Enter the no. of disks to be transferred: 4
RESULT :
Non-Recursive
Move top disk from needle A to needle B Move top disk from needle A to needle C
Move top disk from needle B to needle C Move top disk from needle A to needle B
Move top disk from needle C to needle A Move top disk from needle C to needle B
Move top disk from needle A to needle B Move top disk from needle A to needle C
Move top disk from needle B to needle C Move top disk from needle B to needle A
Move top disk from needle C to needle A Move top disk from needle B to needle C
Move top disk from needle A to needle B Move top disk from needle A to needle C
Move top disk from needle B to needle C
Recursive
Move top disk from needle A to needle C. Move top disk from needle A to needle B.
Move top disk from needle C to needle B. Move top disk from needle A to needle C.
JOGINPALLY B.R.ENGINEERING COLLEGE

Page 14

Move top disk from needle B to needle A. Move top disk from needle B to needle C.
Move top disk from needle A to needle C. Move top disk from needle A to needle B.
Move top disk from needle C to needle B. Move top disk from needle C to needle A.
Move top disk from needle B to needle A. Move top disk from needle C to needle B.
Move top disk from needle A to needle C. Move top disk from needle A to needle B.
Move top disk from needle C to needle B.

To find the total distance travelled by vehicle in t seconds at regular


intervals of time for the given values of u and a (distance ut+1/2 at2 )
Program the find the total distance travelled by vehicle in t seconds at regular intervals of
time for the given values of u and a (distance ut+1/2 at2 )
#include <stdio.h>
#include <math.h>
void main()
{
int tim_intrval, counter,time;
float accl, distance=0, velos;
clrscr();
printf("<===PROGRAM FOR CALC TOTAL DISTANCE TRAVELED BY A VECHIAL==>");
printf("\n\n\n\t\t\tNO OF TIME INTERVALS : ");
scanf("%d",&tim_intrval);
for(counter = 1; counter <= tim_intrval; counter++)
{
printf("\n\t\t\tAT T%d TIME(sec) : ",counter);
scanf("%d",&time);
printf("\t\t\tVELOCITY AT %d sec (m/sec) : ",time);
scanf("%f",&velos);
printf("\t\t\tACCLERATION AT %d sec (m/sec^2): ",time);
scanf("%f",&accl);
distance += (velos*time + (accl*pow(time,2))/2);
}
printf("\n\n\n\tTOTAL DISTANCE TRAVELLED BY VEHICLE IN %d INTERVALS OF TIME :
%.2f",tim_intrval,distance);
getch();
}
OUTPUT
PROGRAM FOR CALCULATE TOTAL DISTANCE TRAVELED BY A VECHICLE

INPUT :

NO OF TIME INTERVALS : 3

AT T1 TIME(sec) : 10
JOGINPALLY B.R.ENGINEERING COLLEGE

Page 15

VELOCITY AT 10 sec (m/sec) : 90


ACCLERATION AT 10 sec (m/sec^2): 2.5
AT T2 TIME(sec) : 20
VELOCITY AT 20 sec (m/sec) : 50
ACCLERATION AT 20 sec (m/sec^2): 3
AT T3 TIME(sec) : 40
VELOCITY AT 40 sec (m/sec) : 90
ACCLERATION AT 40 sec (m/sec^2): 5
RESULT :
TOTAL DISTANCE TRAVELLED BY VEHICLE IN 3 INTERVALS
OF TIME : 10225.00

To perform operation on the two given integer operands


and one operator using switch statement
Program to perform operation on the two given integer operands and one operator using
switch statement.
#include<stdio.h>
#include<conio.h>
void main()
{
int op1,op2,res;
char op;
clrscr();
printf("Enter operator :");
scanf("%c",&op);
printf("Enter two values :");
scanf("%d%d",&op1,&op2);
switch(op)
{
case '+': res=op1+op2;
break;
case '-': res=op1-op2;
break;
case '*': res=op1*op2;
break;
case '/': res=op1/op2;
break;
case '%': res=op1%op2;
break;
default : printf("Invalid choice");
}
JOGINPALLY B.R.ENGINEERING COLLEGE

Page 16

printf("\n%d %c %d = %d",op1,op,op2,res);
getch();
}

OUTPUT
INPUT
Enter operator : +
Enter any two integer values : 30 40
RESULT
30 + 40 = 70

To find the largest and smallest numbers in a list of integers


Program to find the largest and smallest numbers in a list of integers
#include<stdio.h>
#include<conio.h>
#define MAX 10
void main()
{
int a[MAX],i,lar,small,n;
printf("Enter value of n:");
scanf("%d",&n);
printf(Enter %d values,n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
lar=small=a[0];
for(i=0;i<n;i++)
{
if(a[i]>lar)
lar=a[i];
if(a[i]<small)
small=a[i];
}
printf("Large=%d Small=%d",lar,small);
getch();
}
JOGINPALLY B.R.ENGINEERING COLLEGE

Page 17

OUTPUT
INPUT :
Enter the value of n : 5
Enter 5 values
44
55
66
33
22
RESULT :
Large : 66
Small : 22

Addition of two matrices


Addition of two matrices
#include<stdio.h>
#include<conio.h>
void main()
{
int A[5][5],B[5][5],C[5][5],i,j,r1,c1,r2,c2;
clrscr();
printf("\nEnter size of matrix A :");
scanf("%d%d",&r1,&c1);
printf("\nEnter size of matrix B :");
scanf("%d%d",&r2,&c2);
if(r1==r2 && c1==c2)
{
printf("\nEnter values of matrix A\n");
for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
{
printf("A[%d][%d]=",i,j);
scanf("%d",&A[i][j]);
JOGINPALLY B.R.ENGINEERING COLLEGE

Page 18

}
printf("\nEnter values of matrix B9\n");
for(i=0;i<r2;i++)
for(j=0;j<c2;j++)
{
printf("B[%d][%d]=",i,j);
scanf("%d",&B[i][j]);
}
for(i=0;i<r2;i++)
for(j=0;j<c2;j++)
C[i][j]=A[i][j]+B[i][j];
printf("\nMatrix A\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
printf("%d\t",A[i][j]);
printf("\n");
}
printf("\nMatrix B\n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
printf("%d\t",B[i][j]);
printf("\n");
}
printf("\nSum of two matrices A and B \n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
printf("%d\t",C[i][j]);
printf("\n");
}
}
else
printf("Addition not possible");
getch();
}
OUTPUT
INPUT:
Enter size of matrix A : 3 3
Enter size of matrix B : 3 3
Enter the values of matrix A 1 2 3 4 5 6 7 8 9
JOGINPALLY B.R.ENGINEERING COLLEGE

Page 19

Enter the values of matrix B 9 8 7 6 5 4 3 2 1


RESULT:
MATRIX A
123
456
789
MATRIX B
987
654
321
Sum of the two matrices A and B is
MATRIX C
10 10 10
10 10 10
10 10 10

Multiplication of two matrices


Multiplication of two matrices
#include<stdio.h>
#include<conio.h>
void main()
{
int A[5][5],B[5][5],C[5][5],i,j,r1,c1,r2,c2;
clrscr();
printf("\nEnter size of matrix A :");
scanf("%d%d",&r1,&c1);
printf("\nEnter size of matrix B :");
scanf("%d%d",&r2,&c2);
if(r1==r2 && c1==c2)
{
printf("\nEnter values of matrix A\n");
for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
JOGINPALLY B.R.ENGINEERING COLLEGE

Page 20

{
printf("A[%d][%d]=",i,j);
scanf("%d",&A[i][j]);
}
printf("\nEnter values of matrix B9\n");
for(i=0;i<r2;i++)
for(j=0;j<c2;j++)
{
printf("B[%d][%d]=",i,j);
scanf("%d",&B[i][j]);
}
for(i=0;i<r2;i++)
for(j=0;j<c2;j++)
C[i][j]=A[i][j]+B[i][j];
printf("\nMatrix A\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
printf("%d\t",A[i][j]);
printf("\n");
}
printf("\nMatrix B\n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
printf("%d\t",B[i][j]);
printf("\n");
}
printf("\nSum of two matrices A and B \n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
printf("%d\t",C[i][j]);
printf("\n");
}
}
else
printf("Addition not possible");
getch();
}
OUTPUT
INPUT:
Enter size of matrix A : 2 3
JOGINPALLY B.R.ENGINEERING COLLEGE

Page 21

Enter size of matrix B : 3 2


Enter the values of matrix A 1 2 3 4 5 6
Enter the values of matrix B 2 1 2 1 2 1
RESULT:
MATRIX A
1
2
3
4
5
6
MATRIX B
2
1
2
1
2
1
Sum of the two matrices A and B is
MATRIX C
12
6
30
15

To insert a sub-string in to given main string from a given position


Program to insert a sub-string in to given main string from a given position
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int pos;
char mstr[100],substr[10];
void strin(char[],char[],int);
clrscr();
printf("Enter main string");
gets(mstr);
printf("Enter the substr to be inserted:");
gets(substr);
JOGINPALLY B.R.ENGINEERING COLLEGE

Page 22

printf("Enter the position to insert the substring in the mainstring:");


scanf("%d",&pos);
strin(mstr,substr,pos);
getch();
}
void strin(char m[],char sub[],int p)
{
char str1[100];
int i,j,k;
j=0;
for(i=0;i<p;i++)
str1[j++]=m[i];
for(k=0;sub[k]!='\0';k++)
str1[j++]=sub[k];
for(;i<strlen(m);i++)
str1[j++]=m[i];
str1[j]='\0';
printf("Main String after insertion of substring\n%s",str1);
}INPUT:Enter the main string : eslish
Enter the sub string : tab
Enter the position where the item has to be inserted:2
RESULT:
Main string after insertion of substring : establish

To delete n characters from a given position in a given string


Program to delete n characters from a given position in a given string.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int i,pos,len,n;
char mstr[100],substr[10],str1[100];
void strdel(char[],int,int);
clrscr();
printf("Enter main string");
gets(mstr);
printf("\nEnter how many characters and from which position:");
scanf("%d%d",&n,&pos);
strdel(mstr,n,pos);
getch();
}
JOGINPALLY B.R.ENGINEERING COLLEGE

Page 23

void strdel(char m[],int n,int p)


{
int i;
for(i=p-1;i<m[i]!='\0';i++)
m[i]=m[i+n];
printf("After deletion : %s",m);
}

OUTPUT
INPUT:
Enter the first string : establish
Enter how many characters and from which position : 3 2
RESULT:
After deletion the string is elish

To determine if the given string is a palindrome or not


Program to determine if the given string is a palindrome or not
#include<stdio.h>
#include<string.h>
enum Boolean{false,true};
enum Boolean IsPalindrome(char string[])
{
int left,right,len=strlen(string);
enum Boolean matched=true;
if(len==0)
return 0;
left=0;
right=len-1;
/* Compare the first and last letter,second & second last & so on */
while(left<right&&matched)
{
if(string[left]!=string[right])
matched=false;
else
{
left++;
right--;
JOGINPALLY B.R.ENGINEERING COLLEGE

Page 24

}
}
return matched;
}
int main()
{
char string[40];
clrscr();
printf("****Program to test if the given string is a palindrome****\n");
printf("Enter a string:");
scanf("%s",string);
if(IsPalindrome(string))
printf("The given string %s is a palindrome\n",string);
else
printf("The given string %s is not a palindrome\n",string);
getch();
return 0;
}
OUTPUT
INPUT
Enter a string : MALAYALAM
RESULT
The given string MALAYALAM is a palindrome

To display the position or index in the string S


where the string T begins, or - 1 if S doesn't contain T.
Program that displays the position or index in the string S where the string T begins, or - 1 if
S doesn't contain T.
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
char s[30], t[20];
char *found;
clrscr();
/* Entering the main string */
puts("Enter the first string: ");
gets(s);
/* Entering the string whose position or index to be displayed */
JOGINPALLY B.R.ENGINEERING COLLEGE

Page 25

puts("Enter the string to be searched: ");


gets(t);
/*Searching string t in string s */
found=strstr(s,t);
if(found)
printf("Second String is found in the First String at %d position.\n",(found-s)+1);
else
printf("-1");
getch();
}
OUTPUT
INPUT:
Enter the first string : horizontal
Enter the string to be searched : zon
RESULT:
Second String is found in the First String at 5 position.

To count the lines, words and characters in a given text.


program to count the lines, words and characters in a given text.
#include <stdio.h>
main()
{
char line[81], ctr;
int i,c,
end = 0,
characters = 0,
words = 0,
lines = 0;
printf("KEY IN THE TEXT.\n");
printf("GIVE ONE SPACE AFTER EACH WORD.\n");
printf("WHEN COMPLETED, PRESS 'RETURN'.\n\n");
while( end == 0)
JOGINPALLY B.R.ENGINEERING COLLEGE

Page 26

{
/* Reading a line of text */
c = 0;
while((ctr=getchar()) != '\n')
line[c++] = ctr;
line[c] = '\0';
/* counting the words in a line */
if(line[0] == '\0')
break ;
else
{
words++;
for(i=0; line[i] != '\0';i++)
if(line[i] == ' ' || line[i] == '\t')
words++;
}
/* counting lines and characters */
lines = lines +1;
characters = characters + strlen(line);
}
printf ("\n");
printf("Number of lines = %d\n", lines);
printf("Number of words = %d\n", words);
printf("Number of characters = %d\n", characters);
}

OUTPUT
INPUT :
KEY IN THE TEXT.
GIVE ONE SPACE AFTER EACH WORD.
WHEN COMPLETED, PRESS 'RETURN'.
Admiration is a very short-lived passion.
Admiration involves a glorious obliquity of vision.
Always we like those who admire us but we do not
like those whom we admire.
Fools admire, but men of sense approve.
RESULT :
JOGINPALLY B.R.ENGINEERING COLLEGE

Page 27

Number of lines = 5
Number of words = 36
Number of characters = 205

Pascal's triangle
Pascal Triangle
#include<stdio.h>
#include<conio.h>
void main()
{
int bin,p,q,r,x;
clrscr();
bin=1;
q=0;
printf("How many rows you want :");
scanf("%d",&r);
printf("\nPascal's Triangle:\n");
while(q<r)
{
for(p=40-3*q;p>0;--p)
printf(" ");
for(x=0;x<=q;++x)
{
if((x==0)||(q==0))
bin=1;
else
bin=(bin*(q-x+1))/x;
printf("%6d",bin);
}
printf("\n");
++q;
}
getch();
}
OUTPUT:
INPUT:
Enter how many rows you want : 5
RESULT :
JOGINPALLY B.R.ENGINEERING COLLEGE

Page 28

Pascal's Triangle:
1
1
1
1
1

1
2

3
4

1
3

1
4

To construct a pyramid of numbers


program to construct a pyramid of numbers
#include<stdio.h>
#include<conio.h>
void main()
{
int num,i,y,x=35;
clrscr();
printf("\nEnter the number to generate the pyramid:\n");
scanf("%d",&num);
for(y=0;y<=num;y++)
{
/*(x-coordinate,y-coordinate)*/
gotoxy(x,y+1);
/*for displaying digits towards the left and right of zero*/
for(i=0-y;i<=y;i++)
printf("%3d",abs(i));
x=x-3;
}
getch();
}

OUTPUT
INPUT :
Enter the number to generate the : 5
RESULT :
JOGINPALLY B.R.ENGINEERING COLLEGE

Page 29

2
3 2
4 3 2
5 4 3 2

To

1
1
1
1
1

0
0
0
0
0
0

1
1
1
1
1

2
2 3
2 3 4
2 3 4 5

read in two numbers, x and n, and then compute the sum of this
geometric progression: 1+x+x2+x3+. . . xn

program to read in two numbers, x and n, and then compute the sum of this geometric
progression: 1+x+x2+x3+. . . xn
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
long x,n,i,sum=0,v;
clrscr();
lab:
printf("Enter the values of x and n:");
scanf("%ld%ld",&x,&n);
if(n<0)
{
printf("does not take negative exponents");
goto lab;
}
else
for(i=0;i<=n;i++)
{
v=pow(x,i);
printf("+%ld",v);
sum=sum+v;
}
printf("=%ld\n",sum);
getch();
}
OUTPUT
INPUT :Enter the values of x and n : 2

JOGINPALLY B.R.ENGINEERING COLLEGE

8
Page 30

RESULT:
1+2+4+8+16+32+64+128+256=511

To find the 2s complement of a binary number


Program to find the 2s complement of a binary number.
#include <stdio.h>
#include<conio.h>
void complement (char *a);
void main()
{
char a[16];
int i;
clrscr();
printf("Enter the binary number : ");
gets(a);
for(i=0;a[i]!='\0'; i++)
{
if (a[i]!='0' && a[i]!='1')
{
printf("The number entered is not a binary number. Enter the correct number");
exit(0);
}
}
complement(a);
getch();
}
void complement (char *a)
{
int l, i, c=0;
char b[16];
l=strlen(a);
for (i=l-1; i>=0; i--)
{
if (a[i]=='0')
b[i]='1';
else
b[i]='0';
}
JOGINPALLY B.R.ENGINEERING COLLEGE

Page 31

for(i=l-1; i>=0; i--)


{
if(i==l-1)
{
if (b[i]=='0')
b[i]='1';
else
{
b[i]='0';
c=1;
}
}
else
{
if(c==1 && b[i]=='0')
{
b[i]='1';
c=0;
}
else if (c==1 && b[i]=='1')
{
b[i]='0';
c=1;
}
}
}
b[l]='\0';
printf("The 2's complement is %s", b);
}

OUTPUT
INPUT:
Enter the binary number1111
RESULT:
The 2's complement is 0001

To convert a Roman numeral to its decimal equivalent.


JOGINPALLY B.R.ENGINEERING COLLEGE

Page 32

Program to convert a Roman numeral to its decimal equivalent.


#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
void main()
{
int *a,len,i,j,k;
char *rom;
printf("Enter the Roman Numeral:");
scanf("%s",rom);
len=strlen(rom);
for(i=0;i<len;i++)
{
if(rom[i]=='I')
a[i]=1;
else if(rom[i]=='V')
a[i]=5;
else if(rom[i]=='X')
a[i]=10;
else if(rom[i]=='L')
a[i]=50;
else if(rom[i]=='C')
[i]=100;
else if(rom[i]=='D')
a[i]=500;
else if(rom[i]=='M')
a[i]=1000;
else
{
printf("\nInvalid Value");
exit(0);
}
}
k=a[len-1];
for(i=len-1;i>0;i--)
{
if(a[i]>a[i-1])
k=k-a[i-1];
else if(a[i]==a[i-1] || a[i]<a[i-1])
k=k+a[i-1];
}
printf("\nIts Decimal Equivalent is:");
printf("%d",k);
}
JOGINPALLY B.R.ENGINEERING COLLEGE

Page 33

OUTPUT
INPUT:
Enter the Roman Numeral : XXVIII
RESULT:
Its Decimal Equivalent is : 28

Addition and Multiplication of two complex number


Program that uses functions to perform the following operations:
i) Reading a complex number
ii) Writing a complex number
iii) Addition of two complex numbers
iv) Multiplication of two complex number
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct complex
{
float real;
float imag;
};
typedef struct complex c;
void main()
{
int ch;
struct complex add(struct complex,struct complex);
struct complex sub(struct complex,struct complex);
struct complex mul(struct complex,struct complex);
struct complex div(struct complex,struct complex);
JOGINPALLY B.R.ENGINEERING COLLEGE

Page 34

c c1,c2,c3;
clrscr();
printf("Enter first complex ");
scanf("%f%f",&c1.real,&c1.imag);
printf("Enter second complex ");
scanf("%f%f",&c2.real,&c2.imag);
do
{
printf("\n1.Addition");
printf("\n2.Subtraction");
printf("\n3.Multiplication");
printf("\n4.Division");
printf("\n5.exit");
printf("\nEnter ur choice :");
scanf("%d",&ch);
switch(ch)
{
case 1:c3=add(c1,c2);
printf("%.2f+i%.2f",c3.real,c3.imag);
break;
case 2:c3=sub(c1,c2);
printf("%.2f+i%.2f",c3.real,c3.imag);
break;
case 3:c3=mul(c1,c2);
printf("%.2f+i%.2f",c3.real,c3.imag);
break;
case 4:c3=div(c1,c2);
printf("%.2f+i%.2f",c3.real,c3.imag);
break;
case 5:exit(0);
}
}while(ch!=5);
getch();
}
c add(c x,c y)
{
c z;
z.real=x.real+y.real;
z.imag=x.imag+y.imag;
return z;
}
c sub(c x,c y)
{
c z;
z.real=x.real-y.real;
z.imag=x.imag-y.imag;
JOGINPALLY B.R.ENGINEERING COLLEGE

Page 35

return z;
}
c mul(c x,c y)
{
c z;
z.real=(x.real*y.real)-(x.imag*y.imag);
z.imag=(x.real*y.imag)+(x.imag*y.real);
return z;
}
c div(c x,c y)
{
c z;
z.real=((x.real*y.real)+(x.imag*y.imag))/((x.real*x.real)+(x.imag*x.imag));
z.imag=((y.imag*x.real)+(x.real*y.imag))/((x.real*x.real)+(x.imag*x.imag));
return z;
}

OUTPUT
INPUT:
Enter first complex 2.5
Enter second complex 4

6.5
9

RESULT:
1.Addition
2.Subtraction
3.Multiplication
4.Division
5.exit
Enter ur choice :1
Addtion : 6.50+i15.50
1.Addition
2.Subtraction
3.Multiplication
4.Division
5.exit
Enter ur choice :3
JOGINPALLY B.R.ENGINEERING COLLEGE

Page 36

Multiplication : -48.50+i48.50

Binary Search
ALGORITHM :
step 1: start
step 2: read how many elements n
step 3: read n elements
step 4: enter the key element to be searched
step 5: sort the elements
step 6: compare the key element with n elements
step 7:
step 8: write the element is found in postition else not found
step 9: stop
Programs that use both recursive and non recursive functions to perform binary search
operations
#include <stdio.h>
#define MAX_LEN 10
JOGINPALLY B.R.ENGINEERING COLLEGE

Page 37

/* Non-Recursive function*/
void b_search_nonrecursive(int l[],int num,int ele)
{
int l1,i,j, flag = 0;
l1 = 0;
i = num-1;
while(l1 <= i)
{
j = (l1+i)/2;
if( l[j] == ele)
{
printf("\nThe element %d is present at position %d in list\n",ele,j);
flag =1;
break;
}
else
if(l[j] < ele)
l1 = j+1;
else
i = j-1;
}
if( flag == 0)
printf("\nThe element %d is not present in the list\n",ele);
}
/* Recursive function*/
int b_search_recursive(int l[],int arrayStart,int arrayEnd,int a)
{
int m,pos;
if (arrayStart<=arrayEnd)
{
m=(arrayStart+arrayEnd)/2;
if (l[m]==a)
return m;
else if (a<l[m])
return b_search_recursive(l,arrayStart,m-1,a);
else
return b_search_recursive(l,m+1,arrayEnd,a);
}
return -1;
}
void read_list(int l[],int n)
{
int i;
printf("\nEnter the elements:\n");
for(i=0;i<n;i++)
JOGINPALLY B.R.ENGINEERING COLLEGE

Page 38

scanf("%d",&l[i]);
}
void print_list(int l[],int n)
{
int i;
for(i=0;i<n;i++)
printf("%d\t",l[i]);
}
/*main function*/
void main()
{
int l[MAX_LEN], num, ele,f,l1,a;
int ch,pos;
clrscr();
printf("======================================================");
printf("\n\t\t\tMENU");
printf("\n=====================================================");
printf("\n[1] Binary Search using Recursion method");
printf("\n[2] Binary Search using Non-Recursion method");
printf("\n\nEnter your Choice:");
scanf("%d",&ch);
if(ch<=2 & ch>0)
{
printf("\nEnter the number of elements : ");
scanf("%d",&num);
read_list(l,num);
printf("\nElements present in the list are:\n\n");
print_list(l,num);
printf("\n\nEnter the element you want to search:\n\n");
scanf("%d",&ele);
switch(ch)
{
case 1:printf("\nRecursive method:\n");
pos=b_search_recursive(l,0,num,ele);
if(pos==-1)
{
printf("Element is not found");
}
else
{
printf("Element is found at %d position",pos);
}
JOGINPALLY B.R.ENGINEERING COLLEGE

Page 39

getch();
break;
case 2:printf("\nNon-Recursive method:\n");
b_search_nonrecursive(l,num,ele);
getch();
break;
}
}
getch();
}
OUTPUT
INPUT :
==================================
MENU

==================================
[1] Binary Search using Recursion method
[2] Binary Search using Non-Recursion method
Enter your Choice:1
Enter the number of elements : 5
Enter the elements: 45 50 68 74 85
RESULT :
Elements present in the list are:
45
50
68
74
85
Enter the element you want to search : 68
Recursive method:
Element is found at 3 position
INPUT :
======================================================
MENU
=====================================================
[1] Binary Search using Recursion method
[2] Binary Search using Non-Recursion method
Enter your Choice:2
Enter the number of elements : 5
Enter the elements:
JOGINPALLY B.R.ENGINEERING COLLEGE

Page 40

25 65 87 98 153
RESULT :
Elements present in the list are: 25

65

87

98

153

Enter the element you want to search : 98


Non-Recursive method:
The element 98 is present at position 3 in list

Bubble Sort
Definition :
Bubble Sort is an elementary sorting algorithm. It works by repeatedly exchanging adjacent
elements, if necessary. When no exchanges are required, the data is sorted.

ALGORITHM :
Procedure BubbleSort(A, MAX) : Here A is an array consisting of MAX elements. This
procedure sorts the elements in Ascending Order by repeatedly exchanging adjacent elements, if
necessary. Here 'temp' is a temporary variable used to exchange the adjacent elements in this
procedure.

ASCENDING ORDER
for i 1 to MAX do
for j MAX downto i+1 do
If A[j] < A[j-1] then
Exchange A[j] A[j-1]

Program to implement Bubble sort


#include <stdio.h>
#define MAX 10
void swapList(int *m,int *n)
{
int temp;
temp = *m;
*m = *n;
*n = temp;
JOGINPALLY B.R.ENGINEERING COLLEGE

Page 41

}
// Function for Bubble Sort
void bub_sort(int list[], int n)
{
int i,j;
for(i=0;i<(n-1);i++)
for(j=0;j<(n-(i+1));j++)
if(list[j] > list[j+1])
swapList(&list[j],&list[j+1]);
}
void readlist(int list[],int n)
{
int j;
printf("\nEnter the elements: \n");
for(j=0;j<n;j++)
scanf("%d",&list[j]);
}// Showing the contents of the list
void printlist(int list[],int n)
{
int j;
for(j=0;j<n;j++)
printf("%d\t",list[j]);
}
void main()
{
int list[MAX], num;
printf(\nBUBBLE SORT);
printf("\n\n\nEnter the number of elements [Maximum 10]\n");
scanf("%d",&num);
readlist(list,num);
printf("\n\nElements in the list before sorting are:\n");
printlist(list,num);
bub_sort(list,num);
printf("\n\nElements in the list after sorting are:\n");
printlist(list,num);
}
OUTPUT:
******BUBBLE SORT *****
INPUT :
Enter the number of elements [Maximum 10] : 6
JOGINPALLY B.R.ENGINEERING COLLEGE

Page 42

Enter the elements:


55 44 11 66 88 5
RESULT :
Elements in the list before sorting are:
55
44
11 66
88
5
Elements in the list after sorting are:
5
11 44
55
66
88

QUICK SORT
Definition :
Quick Sort works on the principle of divide-and-conquer. It is an algorithm of choice in many
situations because it is not difficult to implement, it is a good "general purpose" sort and it
consumes relatively fewer resources during execution.

ALGORITHM : QUICK SORT


Procedure QuickSort(A, left, right) : Here A is an array consisting of MAX elements. This
procedure sorts the elements in Ascending or Descending Order based on the divide and conquer
method.
Quick Sort works by partitioning a given array A[p . . r] into two non-empty sub-array A[p . . q]
and A[q+1 . . r] such that every key in A[p . . q] is less than or equal to every key in A[q+1 . . r].
Then the two sub-arrays are sorted by recursive calls to Quick sort. The exact position of the
partition depends on the given array and index q is computed as a part of the partitioning
procedure.

Procedure QuickSort(A, left, right)


If left < right then
pivot = Partition (A, left, right)
Recursive call to QuickSort (A, left, pivot-1)
Recursive call to QuickSort (A, pivot+1, right)

PARTITION (A, p, r)
x = A[p]
i = p-1
j = r+1
while TRUE do
JOGINPALLY B.R.ENGINEERING COLLEGE

Page 43

Repeat j = j-1
until A[j] x
Repeat i = i+1
until A[i] x
if i < j
then exchange A[i] = A[j]
else return j

Quick sort
#include <stdio.h>
#define MAX 10
void swap(int *m,int *n)
{
int temp;
temp = *m;
*m = *n;
*n = temp;
}
int get_key_position(int x,int y )
{
return((x+y) /2);
}
// Function for Quick Sort
void quicksort(int list[],int m,int n)
{
int key,i,j,k;
if( m < n)
{
k = get_key_position(m,n);
swap(&list[m],&list[k]);
key = list[m];
i = m+1;
j = n;
while(i <= j)
{
while((i <= n) && (list[i] <= key))
i++;
while((j >= m) && (list[j] > key))
j--;
if( i < j)
swap(&list[i],&list[j]);
}
swap(&list[m],&list[j]);
quicksort(list,m,j-1);
quicksort(list,j+1,n);
JOGINPALLY B.R.ENGINEERING COLLEGE

Page 44

}
}
// Function to read the data
void read_data(int list[],int n)
{
int j;
printf("\n\nEnter the elements:\n");
for(j=0;j<n;j++)
scanf("%d",&list[j]);
}
// Function to print the data
void print_data(int list[],int n)
{
int j;
for(j=0;j<n;j++)
printf("%d\t",list[j]);
}
void main()
{
int list[MAX], num;
clrscr();
printf("\n***** Enter the number of elements Maximum [10] *****\n");
scanf("%d",&num);
read_data(list,num);
printf("\n\nElements in the list before sorting are:\n");
print_data(list,num);
quicksort(list,0,num-1);
printf("\n\nElements in the list after sorting are:\n");
print_data(list,num);
getch();
}
OUTPUT:
******QUICK SORT *****
INPUT :
Enter the number of elements [Maximum 10] : 10
Enter the elements:
15 4 11 6 28 25 9 35 45 3
RESULT :
JOGINPALLY B.R.ENGINEERING COLLEGE

Page 45

Elements in the list before sorting are:


15 4 11 6 28 25 9 35 45
Elements in the list after sorting are:
3 4 6 9 11 15 25 28 35

3
45

Insertion Sort
Definition :
Insertion Sort is an example of an incremental algorithm. It builds the sorted sequence one
number at a time.
If the first few objects are already sorted, an unsorted object can be inserted in the sorted set in
proper place. This is called insertion sort. An algorithm consider the elements one at a time,
inserting each in its suitable place among those already considered (keeping them sorted).

ALGORITHM :
Procedure InsertionSort(A, MAX) : Here A is an array consisting of MAX elements. This
procedure sorts the elements in Ascending Order.

ASCENDING ORDER
For j = 2 to length [A] do
key = A[j]
Put A[j] into the sorted sequence A[1 . . j-1]
i = j-1
while i > 0 and A[i] > key do
A[i+1] = A[i]
i = i-1
A[i+1] = key
Program to implement Insertion sort
#include<stdio.h>
#include<conio.h>
void inst_sort(int []);
void main()
{
JOGINPALLY B.R.ENGINEERING COLLEGE

Page 46

int num[5],count;
clrscr();
printf(\n******INSERTION SORT******);
printf("\nEnter the Five Elements to sort:\n");
for (count=0;count<5;count++)
scanf("%d",&num[count]);
inst_sort(num);
printf("\n\nElements after sorting: \n");
for(count=0;count<5;count++)
printf("%d\n",num[count]);
getch();
}
// Function for Insertion Sorting
void inst_sort(int num[])
{
int i,j,k;
for(j=1;j<5;j++)
{
k=num[j];
for(i=j-1;i>=0 && k<num[i];i--)
num[i+1]=num[i];
num[i+1]=k;
}}
OUTPUT:
*****INSERTION SORT********
INPUT :
Enter the Five Elements to sort:
5
66
22
44
55
RESULT :
Elements after sorting:
5
22
44
55
66

Merge Sort
Definition :
Merge-sort is based on the divide-and-conquer paradigm. The Merge-sort algorithm can be
described in general terms as consisting of the following three steps:
1. Divide Step
JOGINPALLY B.R.ENGINEERING COLLEGE

Page 47

If given array A has zero or one element, return S; it is already sorted. Otherwise, divide A into two
arrays, A1 and A2, each containing about half of the elements of A.
2. Recursion Step
Recursively sort array A1 and A2.
3. Conquer Step
Combine the elements back in A by merging the sorted arrays A1 and A2 into a sorted sequence.

ALGORITHM :
To begin, suppose that we have two sorted arrays A1[1], A1[2], . . , A1[M] and A2[1], A2[2], . . . ,
A2[N]. The following is a direct algorithm of the obvious strategy of successively choosing the
smallest remaining elements from A1 to A2 and putting it in A

MERGE (A1, A2, A)


i = j1
A1[m+1], A2[n+1] = INT_MAX
For k = 1 to m + n do
if A1[i] < A2[j]
then A[k] = A1[i]
i = i +1
else
A[k] = A2[j]
j=j+1
The Merge sort algorithm is as follows:

MERGE_SORT (A)
A1[1 . . floor(n/2)] = A[1 . . floor(n/2)]
A2[1 . . floor(n/2)] = A[1 + floor(n/2) . . n]
Merge Sort (A1)
Merge Sort (A1)
Merge Sort (A1, A2, A)

Program to implement Merge sort


#include <stdio.h>
#include <stdlib.h>
#define MAX_ARY 10
void merge_sort(int x[], int end, int start);
int main(void) {
JOGINPALLY B.R.ENGINEERING COLLEGE

Page 48

int ary[MAX_ARY];
int j = 0;
printf("\n\nEnter the elements to be sorted: \n");
for(j=0;j<MAX_ARY;j++)
scanf("%d",&ary[j]);
/* array before mergesort */
printf("Before :");
for(j = 0; j < MAX_ARY; j++)
printf(" %d", ary[j]);
printf("\n");
merge_sort(ary, 0, MAX_ARY - 1);
/* array after mergesort */
printf("After Merge Sort :");
for(j = 0; j < MAX_ARY; j++)
printf(" %d", ary[j]);
printf("\n");
getch();
}
/* Method to implement Merge Sort*/
void merge_sort(int x[], int end, int start) {
int j = 0;
const int size = start - end + 1;
int mid = 0;
int mrg1 = 0;
int mrg2 = 0;
int executing[MAX_ARY];
if(end == start)
return;
mid = (end + start) / 2;
merge_sort(x, end, mid);
merge_sort(x, mid + 1, start);
for(j = 0; j < size; j++)
executing[j] = x[end + j];
mrg1 = 0;
mrg2 = mid - end + 1;
for(j = 0; j < size; j++) {
if(mrg2 <= start - end)
if(mrg1 <= mid - end)
if(executing[mrg1] > executing[mrg2])
x[j + end] = executing[mrg2++];
else
x[j + end] = executing[mrg1++];
else
x[j + end] = executing[mrg2++];
else
x[j + end] = executing[mrg1++];
}
JOGINPALLY B.R.ENGINEERING COLLEGE

Page 49

}
OUTPUT:
*******MERGE SORT**********
Enter 10 elements to be sorted:
55
66
22
33
1
44
77
58
25
35
Before : 55 66 22 33 1 44 77 58 25 35
After Merge Sort : 1 22 25 33 35 44 55 58 66 77

JOGINPALLY B.R.ENGINEERING COLLEGE

Page 50

You might also like