You are on page 1of 49

PROGRAM NO:- 1

OBJECTIVE:-

Write a program to print the Factorial of a Number.

PROGRAM CODE:-

#include<stdio.h>

#include<conio.h>

void fact(int);

void main()

int num;

clrscr();

printf("\t\tPROGRAM OF FACTOTIAL OF A NUMBER. \n");

printf("\t\Rituraj Sharma.\n\t\t1634210055.\n");

printf("\nEnter the number : ");

scanf("\n%d",&num);

fact(num);

getch();

void fact(int num)

int f=1,i;

for(i=1;i<=num;i++)

f=f*i;

printf("Factorial = %d",f);}
OUTPUT

PROGRAM OF FACTOTIAL OF A NUMBER.

Rituraj Sharma.

1634210055.

Enter the number : 7

Factorial = 5040
PROGRAM NO:- 2

OBJECTIVE:-

Write a program to print the factorial of a Number using Rrecursion.

PROGRAM CODE:-

#include<stdio.h>

#include<conio.h>

int fact(int);

void main()

int f,num;

clrscr();

printf("\t\tPROGRAM OF FACTOTIAL OF A NUMBER USING RECURSION. \n");

printf("\t\tRituraj Sharma. \n\t\t1634210055.\n");

printf("\nEnter the number : ");

scanf("\n%d",&num);

f=fact(num);

printf("Factorial = %d",f);

getch();

int fact(int num)

if(num<1)
return 1;

else

return(num*fact(num-1));

OUTPUT

PROGRAM OF FACTOTIAL OF A NUMBER USING RECURSION.

Rituraj Sharma.

1634210055.

Enter the number : 6

Factorial = 720
PROGRAM NO:- 3

OBJECTIVE:-

Write a program to print the sum of Natural Numbers using for Loop.

PROGRAM CODE:-

#include<stdio.h>

#include<conio.h>

void sum(int);

void main()

int num;

clrscr();

printf("\t\tPROGRAM OF SUM OF NATURAL NUMBERS. \n");

printf("\t\tRituraj Sharma.\n\t\t1634210055.\n");

printf("\nEnter upto which number : ");

scanf("\n%d",&num);

sum(num);

getch();

int sum(int num)

int sum=0,i;

for(i=1;i<=num;i++)

sum=sum+i;

printf("Sum = %d",sum);

}
OUTPUT

PROGRAM OF SUM OF NATURAL NUMBERS.

Rituraj Sharma.

1634210055.

Enter upto which number : 10

Sum = 55
PROGRAM NO:- 4

OBJECTIVE:-

Write a program to print the sum of Natural Numbers using Recursion.

PROGRAM CODE:-

#include<stdio.h>

#include<conio.h>

int sum(int);

void main()

int s,num;

clrscr();

printf("\t\tPROGRAM OF SUM OF NATURAL NUMBERS USING RECURSION.


\n");

printf("\nRituraj Sharma.\n1634210055.\n");

printf("\nEnter upto which number : ");

scanf("%d",&num);

s=sum(num);

printf("Sum = %d",s);

getch();

int sum(int num)

if(num>0)

return (num+sum(num-1));

else

return num;

}
OUTPUT

PROGRAM OF SUM OF NATURAL NUMBERS USING RECURSION.

Rituraj Sharma.

1634210055.

Enter the number : 20

Factorial = 210
PROGRAM NO:-5

OBJECTIVE:-

Write a program to print the Fibonacci Series.

PROGRAM CODE:-

#include<stdio.h>

#include<conio.h>

int fib(int);

void main()

int num;

clrscr();

printf("\t\tPROGRAM TO PRINT FIBONACCI SERIES.\n");

printf("\nRituraj Sharma.\n\1634210055.\n");

printf("\nEnter upto the number : ");

scanf("%d",&num);

fib(num);

getch();

int fib(int num)

int a=-1,b=1,c,i;

for(i=1;i<=num;i++)

c=a+b;

printf("%d ",c);

a=b;
b=c;

OUTPUT

PROGRAM TO PRINT FIBONACCI SERIES.

Rituraj Sharma.

1634210055.

Enter upto the number : 8

0 1 1 2 3 5 8 13
PROGRAM NO:-6

OBJECTIVE:-

Write a program to print the Fibonacci Series using Recursion.

PROGRAM CODE:-

#include<stdio.h>

#include<conio.h>

int fib(int);

void main()

int f,num,i

clrscr();

printf("\t\tPROGRAM TO PRINT FIBONACCI SERIES USING RECURSION.\n");

printf("\t\tRituraj Sharma.\n\t\t1634210055.\n");

printf("\nEnter upto the number : ");

scanf("\n%d",&num);

for(i=1;i<=num;i++)

printf("%d ",fib(i));

getch();

int fib(int num)

if(num<2)

return num;

else
{

return fib(num-1)+fib(num-2);

OUTPUT

PROGRAM TO PRINT FIBONACCI SERIES USING RECURSION .

Rituraj Sharma.

1634210055.

Enter upto the number : 8

0 1 1 2 3 5 8 13
PROGRAM NO:- 7

OBJECTIVE:-

Write a program to implement Insertion Sort.

PROGRAM CODE:-

#include<stdio.h>

#include<conio.h>

void insertion(int [],int);

int main()

int a[20],n,i,j,key;

printf("PROGRAM OF INSERTION SORT. \n");

printf("Rituraj Sharma.\n1634210055.\n");

printf("\nEnter the size of array:");

scanf("%d",&n);

printf("\nEnter array elements");

for(i=1;i<=n;i++);

scanf("%d",&a[i]);

printf("\nAfter sorting");

insertion(a,n);

getch();

void insertion(int a[],int n)

int i,j,key;

for(j=2;j<=n;j++)

{
key=a[j];

i=j-1;

while((i>0)&&(a[i]>key))

{ a[i+1]=a[i];

i=i-1;

a[i+1]=key;

for(i=1;i<=n;i++)

printf(" %d",a[i]);

OUTPUT

PROGRAM OF INSERTION SORT.

Rituraj Sharma.

1634210055.

Enter the size of array 5

Enter array elements

After sorting : 4 5 6 7 8
PROGRAM NO:-8

OBJECTIVE:-

Write a program to Implement the Quick Sort.

PROGRAM CODE:-

#include<stdio.h>

#include<conio.h>

int n,p,r,q,i;

int partition(int[],int,int);

void quick_sort(int [],int,int);

void main()

int a[20];

clrscr();

printf("PROGRAM OF QUICK SORT. \n");

printf("Rituraj Sharma.\n1634210055.\n");

printf("\nEnter the size of array");

scanf("\n%d",&n);

printf("\nEnter array elements:");

for(i=1;i<=n;i++)

scanf("%d",&a[i]);

p=1;

r=n;

quick_sort(a,p,r);

for(i=1;i<=n;i++)

printf("%d ",a[i]);

getch();}
void quick_sort(int a[],int p,int r)

if(p<r)

q=partition(a,p,r);

quick_sort(a,p,q-1);

quick_sort(a,q+1,r);

int partition(int a[],int p,int r)

int x,j,temp;

x=a[r];

i=p-1;

for(j=p;j<=r-1;j++)

if(a[j]<=x)

i=i+1;

temp=a[i];

a[i]=a[j];

a[j]=temp;

temp=a[i+1];

a[i+1]=a[r];

a[r]=temp;

return i+1;}
OUTPUT

PROGRAM OF QUICK SORT.

Rituraj Sharma.

1634210055.

Enter the size of array: 5

Enter array elements:

sorted array is: 2 3 5 7 8


PROGRAM NO:-9

OBJECTIVE:-

Write a program to Implement Count Sort.

PROGRAM CODE:-

#include<stdio.h>

#include<conio.h>

int n;

void count_sort(int [],int [],int);

int main()

int a[20],i,j,k,b[10];

clrscr();

printf("\t\tPROGRAM OF COUNT SORT. \n");

printf("Rituraj Sharma.\n1634210055.\n");

printf("\nEnter the size of array:");

scanf("\n%d",&n);

printf("\nEnter array elements:");

for(i=1;i<=n;i++)

scanf("%d",&a[i]);

k=a[1];

for(i=1;i<=n;i++)

if(a[i]>k)

{
k=a[i];

printf("%d",k);

printf("\nAfter sorting");

count_sort(a,b,k);

getch();

void count_sort(int a[],int b[],int k)

int i,j,c[10];

for(i=0;i<=k;i++)

c[i]=0;

for(j=1;j<=n;j++)

c[a[j]]=c[a[j]]+1;

for(i=1;i<=k;i++)

c[i]=c[i]+c[i-1];

for(j=n;j>=1;j--)

b[c[a[j]]]=a[j];

c[a[j]]=c[a[j]]-1;
}

for(j=1;j<=n;j++)

printf(" %d",b[j]);

OUTPUT

PROGRAM OF COUNT SORT.

Rituraj Sharma.

1634210055.

Enter the size of array: 5

Enter array elements :

After sorting : 5 6 7 8 9
PROGRAM NO:- 10

OBJECTIVE:-

Write a program to Implement Merge Sort.

PROGRAM CODE:-

#include<stdio.h>

#include<conio.h>

merge(int a[],int p,int q,int r)

int i,n1,n2,j,l[20],ri[20],k;

n1=q-p+1;

n2=r-q;

for(i=1;i<=n1;i++)

l[i]=a[p+i-1];

for(j=1;j<=n2;j++)

ri[j]=a[q+j];

l[n1+1]=999;

ri[n2+1]=999;

i=1;

j=1;

for(k=p;k<=r;k++)

if(l[i]<=ri[j])

a[k]=l[i];

i++;
else

a[k]=ri[j];

j++;

void merge_sort(int a[],int p,int r )

int q;

if(p<r)

q=(p+r)/2;

merge_sort(a,p,q);

merge_sort(a,q+1,r);

merge(a,p,q,r);

int main()

int a[20],p,r,n,i;

printf("PROGRAM OF MERGE SORT. \n");

printf("Rituraj Sharma.\n1634210055.\n");

printf("Enter number of element:-");

scanf("%d",&n);

printf("Enter the array:-\n");

for(i=0;i<n;i++)

scanf("%d",&a[i]);
p=0;

r=n-1;

merge_sort(a,p,r);

printf("sorted array is:-\n");

for(i=0;i<n;i++)

printf("%d ",a[i]);

getch();

OUTPUT

PROGRAM OF MERGE SORT.

Rituraj Sharma.

1634210055.

Enter the size of array: 5

Enter array elements :

After sorting : 4 5 6 7 8
PROGRAM NO:-11

OBJECTIVE:-

Write a Program to implement Heap Sort.

PROGRAM CODE:-

#include<stdio.h>

#include<conio.h>

void main()

int heap[10], no, i, j, c, root, temp;

clrscr();

printf("\t\tPROGRAM OF HEAP SORT.");

printf("\nRituraj Sharma.\n1634210055.\n");

printf("\nEnter no of elements :");

scanf("\n%d", &no);

printf("\nEnter the Elements : \n");

for (i = 0; i < no; i++)

scanf("\n%d", &heap[i]);

for (i = 1; i < no; i++)

c = i;

do

root = (c - 1) / 2;

if (heap[root] < heap[c])


{

temp = heap[root];

heap[root] = heap[c];

heap[c] = temp;

c = root;

} while (c != 0);

for (j = no - 1; j >= 0; j--)

temp = heap[0];

heap[0] = heap[j];

heap[j] = temp;

root = 0;

do

c = 2 * root + 1;

if ((heap[c] < heap[c + 1]) && c < j-1)

c++;

if (heap[root]<heap[c] && c<j)

temp = heap[root];

heap[root] = heap[c];

heap[c] = temp;

root = c;

} while (c < j);


}

printf("\nThe sorted array is : ");

for (i = 0; i < no; i++)

printf("\t %d", heap[i]);

getch();

OUTPUT

PROGRAM OF HEAP SORT.

Rituraj Sharma.

1634210055.

Enter no of elements :5

Enter the Elements :

20

10

500

200

100

The sorted array is : 10 20 100 200 500


PROGRAM NO:-12

OBJECTIVE:-

Write a Program to implement Radix Sort.

PROGRAM CODE:-

#include <stdio.h>

#include <conio.h>

int largest (int arr[], int n);

void radix_sort (int arr[], int n);

void main ()

int arr[10], i, n , j ,k;

clrscr ();

printf("\t\tPROGRAM FOR RADIX SORT.");

printf("\nRituraj Sharma.\n1634210055.\n");

printf("\n Enter the number of elements in the array : " );

scanf("%d", &n);

printf("\n Enter the elements of the array");

for(i = 0;i < n;i++)

printf("\n arr[%d] = ", i);

scanf ("%d", &arr [i]);

radix_sort (arr, n);

printf ("\n The sorted array is: \n" );

for(i = 0;i < n;i++)

{
printf ("%d\t", arr [i]);

getch ();

int largest (int arr[], int n)

int large=arr[0], i;

for(i = 1;i < n;i++)

if(arr[i]> large)

large = arr[i];

return large;

void radix_sort (int arr[], int n)

int bucket [10] [10], bucket_count [10];

int i, j , k , remainder, NOP=0, divisor=1, large, pass;

large = largest (arr, n);

while (large > 0)

NOP++;

large/=10;

for(pass = 0;pass < NOP;pass++)

for(i = 0;i < 10;i++)


{

bucket_count[i]=0;

for(i = 0;i < n;i++)

remainder= (arr[i]/divisor)%10;

bucket [remainder] [bucket_count[remainder]] = arr[i];

bucket_count[remainder] += 1;

i=0;

for(k=0;k < 10;k++)

for(j=0;j< bucket_count[k];j++)

arr [i] = bucket [k] [j];

i++;

divisor *= 10;

getch();

}
OUTPUT

PROGRAM FOR RADIX SORT.

Rituraj Sharma.

1634210055.

Enter the number of elements in the array : 5

Enter the elements of the array

arr[0] = 509

arr[1] = 407

arr[2] = 50

arr[3] = 29

arr[4] = 40

The sorted array is:

29 40 50 407 509
PROGRAM NO :-13

OBJECTIVE:-
WAP to Implement Bucket Sort.

PROGRAM CODE:-
#include <stdio.h>
#include<conio.h>
void Bucket_Sort(int array[], int n)
{
int i, j;

int count[n];

for (i = 0; i < n; i++)

count[i] = 0;

for (i = 0; i < n; i++)

(count[array[i]])++;

for (i = 0, j = 0; i < n; i++)

for(; count[i] > 0; (count[i])--)

array[j++] = i;
}

int main()
{
int array[100], i, num;

printf(“\t\tPROGRAM FOR BUCKET SORT.”);

printf(“\nRituraj Sharma.\n1634210055.\n”);

printf("Enter the size of array : ");

scanf("%d", &num);
printf("Enter the %d elements to be sorted:\n",num);

for (i = 0; i < num; i++)

scanf("%d", &array[i]);

printf("\nThe array of elements before sorting : \n");

for (i = 0; i < num; i++)

printf("%d ", array[i]);

printf("\nThe array of elements after sorting : \n");

Bucket_Sort(array, num);

for (i = 0; i < num; i++)

printf("%d ", array[i]);

printf("\n");

getch();}

OUTPUT

PROGRAM FOR BUCKET SORT.

Rituraj Sharma.

1634210055.

Enter How many Numbers : 10

Enter the 10 elements to be sorted : 8 1 3 2 1 5 4 9 6 7

The array of elements before sorting :

8 1 3 2 1 5 4 9 6 7

The array of elements after sorting :

1 1 2 3 4 5 6 7 8 9
PROGRAM NO:-14

OBJECTIVE :-
W.A.P to Implement sequential search.

PROGRAM CODE:-

#include<stdio.h>

#include<conio.h>

#define max 20

void main()
{

int a[max],i,num,c,flag=0;

clrscr();

printf(“\t\tPROGRAM TO IMPLEMENT SEQUIENCIAL SEARCH.\n”);

printf("\t\t Rituraj Sharma.\n\t\t1634210055.\n”);

printf("Enter the length\n");

scanf("%d",&num); printf("\n Enter the elements in the array\n");

for(i=0;i<num;i++)

scanf("%d",&a[i]);

printf("\n Enter the number that to be searched");

scanf("%d",&c);

for(i=0;i<num;i++){

if(a[i]==c){

printf("element found at pos %d”,i+1 ");


flag==1
}

if(flag==0) printf("\n not found");getch();}

OUTPUT

PROGRAM TO IMPLEMENT SEQUENTIAL SEARCH.

Rituraj Sharma.

1634210055.

Enter the length

Enter the elements in the array

Enter the number that to be searched

element found at pos 3


PROGRAM NO:-15

OBJECTIVE:-
W.A.P to implement Binary search.

PROGRAM CODE:-

#include<stdio.h>
#include<conio.h>

#define max 20
void main()
{

int a[max],i,p=0,n,num,j,mid,x=0;
clrscr();

printf(“Program to implement Binary search\n");

printf("\t\tRituraj Sharma.\t\t1634210055.\n”);

printf("Enter the length");

scanf("%d",&n);

printf("\n Enter the elements in sorted order");

for(i=0;i<n;i++) scanf("%d",&a[i]);

printf("\n Enter the number that to be searched");

scanf("%d",&num);

for(i=p;i<=n+1;i++)
{

mid=(p+n)/2;

if(num==a[mid])
{
printf("\n element found"); x=1; break;
}
if(num>a[mid]) p=mid+1; else

if(num<a[mid]) n=mid;
}

if(x==0) printf("\n not found"); getch();

OUTPUT

PROGRAM TO IMPLEMENT BINARY SEARCH.

Rituraj Sharma.

1634210055.

Enter the length

Enter the elements in sorted order

Enter the number to be searched

element found
PROGRAM NO.16

OBJECTIVE:-
W.A.P to implement recursive sequential search

PROGRAM CODE:-
#include<conio.h>

#include<stdlib.h>

int linear(int[],int,int);

void main()

int item,i,arr[5],s;

clrscr();

printf(“Program to implement Recursive sequential search\n");

printf("\t\tRituraj Sharma.\n\t\t1634210055\n”) ;

printf("Enter the elements\n");

for(i=0;i<5;i++)

scanf("%d",&arr[i]);

printf("\n Enter the number that to be searched");

scanf("%d",&item);

s=4;

linear(arr,s,item);

getch();

int linear(int arr[],int s,int item)


{

if(arr[s]==item)

{printf("\n element found");exit(1);getch();}

else if(s<0)

printf("\n not found");

exit(1);

getch();

return linear(arr,s-1,item);
}

OUTPUT

PROGRAM TO IMPLEMENT RECURSIVE SEQUENTIAL SEARCH.

Rituraj Sharma.

1634210055.

Enter the elements

7 6 0 2 8

Enter the number to be searched

element found
PROGRAM NO:-17

OBJECTIVE:-
W.A.P to implement Recursive Binary search.

PROGRAM CODE:-

#include<stdio.h>

#include<conio.h>

int binarysearch(int [],int,int,int);

void main()

int a[10];

int num,p,number;

clrscr();

printf(“Program to implement Recursive binary search\n");

printf("\t\t Rituraj Sharma.\n\t\t1634210055.\n”);

printf("Enter the length\n");

scanf("%d",&num);

printf("Enter the elements in the array\n");

for (int i=0; i<num; i++)

scanf("%d",&a[i]);

printf("Enter the element to be searched\n");

scanf("%d",&number);
p=binarysearch(a,0,l-1,number);
if (p<0 )

printf("not found”);

else

printf(“ element found “);

getch();}

int binarysearch(int a[],int l,int h,int x)

int mid;

mid=(l+h)/2;

if (l>h)

return -1;

if (a[mid]==x)

return mid;

if (a[mid]<x)

return binarysearch(a,mid+1,h,x);

else

return binarysearch(a,l,mid-1,x);

}
OUTPUT

PROGRAM TO IMPLEMENT RECURSIVE BINARY SEARCH.

Rituraj Sharma.

1634210055.

Enter the length

Enter the elements in the array

Enter the element to be searched

element found
PROGRAM NO:18

OBJECTIVE:-
W.A.P to implement kth Smallest element.

PROGRAM CODE:-

#include<stdio.h>

#include<conio.h>

int N = 20;

int A[20];

void swap(int dex1, int dex2) {

int temp = A[dex1];

A[dex1] = A[dex2];

A[dex2] = temp;
}

int partition(int start, int end) {

int i = start + 1;

int j = i;

int pivot = start;

for (; i < end; i++) {

if (A[i] < A[pivot]) {

swap(i, j);

j++;
}
}

if (j <= end)
swap(pivot, (j - 1));

return j - 1;
}

void quick_sort(int start, int end, int K) {

int part;

if (start < end) {

part = partition(start, end);

if (part == K - 1)

printf("kth smallest element : %d ", A[part]);

if (part > K - 1)

quick_sort(start, part, K);

else

quick_sort(part + 1, end, K);


}

return;
}

int main(int argc, char **argv) {

int i;

printf(“\t\t PROGRAM FOR KTH SMALLEST ELEMENT.\n”);

printf(“\t\tRituraj Sharma.\n\t\t1634210055.\n”);

time_t seconds;

time(&seconds);

srand((unsigned int) seconds);


for (i = 0; i < N; i++)

A[i] = rand() % (1000 - 1 + 1) + 1;


printf("The original sequence is: ");

for (i = 0; i < N; i++)

printf("%d ", A[i]);

printf("\nEnter the Kth smallest you want to find: ");

int k;

scanf("%d", &k);

quick_sort(0, N, k);

getch();}

OUTPUT

PROGRAM FOR Kth SMALLEST ELEMENT.

Rituraj Sharma.

1634210055.

The original sequence is: 909 967 552 524 735 383 616 718 904 945

730 173 143 954 482 307 228 35 224 703

Enter the Kth smallest you want to find: 3

kth smallest element : 173


PROGRAM NO:-19

OBJECTIVE

W.A.P to implement Bubble sort

PROGRAM CODE:-

#include<stdio.h>

#include<conio.h>

void main()

int a[5],i,j,temp;

clrscr();

printf(“Program to implement Bubble sort\n");

printf("\t\tRituraj Sharma.\n\t\t1634210055.\n” ) ;

printf(“Enter the elements \n”);

for(i=0;i<5;i++)

scanf("%d",&a[i]);

printf("\n Element after sorting\n");

for(i=0;i<5;i++)

for(j=0;j<4;j++)

{
if(a[j]>a[j+1])

temp=a[j];

a[j]=a[j+1];

a[j+1]=temp;

}}

for(i=0;i<5;i++)

printf("\n%d",a[i]);

getch();

OUTPUT

PROGRAM TO IMPLEMENT BUBBLE SORT.

Rituraj Sharma.

1634210055.

Enter the elements in the array :

1 9 4 3 10

element after sorting :

1 3 4 9 10
PROGRAM NO:-20

OBJECTIVE:-

W.A.P to implement selection sort.

PROGRAM CODE:-

#include <stdio.h>

#include<conio.h>

void main()

int a[5],n,i,j,s,temp,b;

clrscr();

printf(“ Program to implement Selection sort\n");

printf("\t\tRituraj Sharma.\n\t\t1634210055\n” ) ;

printf("\nEnter the elements \n");

for(i=0;i<5;i++)

scanf("%d",&a[i]);

printf("\nElement after sorting");

for(i=0;i<5;i++)

n=a[i];

for(j=i+1;j<5;j++){
if(n>a[j]) {

temp=a[j];

s=j;

n=temp;} }

if(n<a[i]) {

b=a[i];

a[i]=temp;

a[s]=b;}}

for(i=0;i<5;i++)

printf("\n%d",a[i]);

getch();

OUTPUT

PROGRAM TO IMPLEMENT SELECTION SORT.

Rituraj Sharma.

1634210055.

Enter the elements :

1 3 2 9 8

Elements after sorting :

1 2 3 8 9

You might also like