Data STR Pratical File
Data STR Pratical File
(Estd.By Govt,of Uttarakhand,vide Shri Guru Ram Rai Univarsity Act No.03 of 2017)
Patel Nagar,Dehradun-248001,Uttarakhand
SUBMITTED BY SUBMITTED TO
DEEPAK SINGH Mr.Vaibhav sharma
COURSE:BSC.IT(IInd sem) (Asst.professor)
1|Page
1. ‘C’ Program to find sum of all elements in an array.
#include<stdio.h>
#include<conio.h>
void main()
clrscr();
int a[20],i,n,sum=0;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
sum+=a[i];
getch();
}
2|Page
2. ‘ C’ Program to multiply two matrices with the help of 2D arrays.
#include<stdio.h>
#include<conio.h>
#define r1 3
#define c1 3
#define r2 3
#define c2 3
void main()
int a[r1][c1],b[r2][c2],c[r1][c2];
int i,j,k;
clrscr();
for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
scanf("%d",&a[i][j]);
for(i=0;i<r2;i++)
for(j=0;j<c2;j++)
scanf("%d",&b[i][j]);
for(i=0;i<r1;i++)
3|Page
{
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];
printf("matrix is\n");
for(i=0;i<r1;i++)
for(j=0;j<c2;j++)
printf("%d\t",c[i][j]);
printf("\n");
getch();
4|Page
OUTPUT:
5|Page
3. ‘C’ Program to find transpose of a matrix
#include<stdio.h>
#include<conio.h>
void main()
int a[10][10],b[10][10];
int r,c,i,j;
clrscr();
scanf("%d%d",&r,&c);
for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf("%d",&a[i][j]);
for(i=0;i<r;i++)
for(j=0;j<c;j++)
b[j][i]=a[i][j];
printf("transpose of matrix:\n");
for(i=0;i<c;i++)
for(j=0;j<r;j++)
printf("%d\t",b[i][j]);
printf("\n");
getch();
6|Page
OUTPUT
7|Page
4. ‘C’ Program to find reverse of a string.
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
char a[20];
int i,len;
clrscr();
gets(a);
len=strlen(a);
for(i=len;i>=0;i--)
if(a[i]==' ')
a[i]='\0';
printf("%s\t",&(a[i])+1);
printf("%s\n",a);
getch();
8|Page
OUTPUT
9|Page
5. To search a number in an array using Linear Search Method
#include<stdio.h>
#include<conio.h>
void main()
clrscr();
int a[10]={6,1,2,5,8,10,12,13,16};
int item,i;
scanf("%d",&item);
for(i=0;i<=8;i++)
if(item==a[i])
break;
if(i<9)
else
getch();
OUTPUT
10 | P a g e
6. ‘C’ Program to count total number of vowel and consonants in a string.
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
int i=0,vowels=0,consonants=0;
char a[100];
clrscr();
gets(a);
while(a[i]!='\0')
if(a[i]=='a'|| a[i]=='A'||
a[i]=='e'||a[i]=='E'||
a[i]=='i'||a[i]=='I'||
a[i]=='o'||a[i]=='O'||
a[i]=='u'||a[i]=='U')
vowels++;
else
consonants++;
i++;
printf("\n no of vowels:%d",vowels);
11 | P a g e
printf("\n no of consonants:%d",consonants);
getch();
OUTPUT:
12 | P a g e
7. Program to search a number using Binary Search Method.
#include<stdio.h>
#include<conio.h>
void main()
int n,a[100],i,high=5-1,low=0,mid,ele;
clrscr();
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
scanf("%d",&ele);
while(low<=high)
mid=(low+high)/2;
if(a[mid]<ele)
low=mid+1;
if(a[mid]==ele)
13 | P a g e
break;
if(ele<a[mid])
high=mid-1;
getch();
Output:
14 | P a g e
8. Program of sorting using bubble sort
#include<stdio.h>
#include<conio.h>
void main()
int a[100],i,j,temp,n;
clrscr();
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
for(j=0;j<n-i-1;j++)
if(a[j]>a[j+1])
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
15 | P a g e
printf("sorted array is:\n");
for(i=0;i<n;i++)
printf("%d",a[i]);
printf("\n");
getch();
16 | P a g e
9. Program to sort an array using selection sort
#include<stdio.h>
#include<conio.h>
void main()
int a[100],i,j,min,temp,n;
clrscr();
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
min=1;
for(j=i+1;j<n;j++)
if(a[j]<a[min]);
min=j;
temp=a[min];
a[min]=a[i];
a[i]=temp;
17 | P a g e
printf("sorted array is\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
getch();
18 | P a g e
10. Program to sort an array using insertion sort
#include<stdio.h>
#include<conio.h>
void main()
int a[100],i,j,n,temp;
clrscr();
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=1;i<=n-1;i++)
j=i;
temp=a[j];
a[j]=a[j-1];
a[j-1]=temp;
j--;
printf("after sorting\n");
19 | P a g e
for(i=0;i<=n-1;i++)
printf("%d\t",a[i]);
getch();
20 | P a g e
11. Program to sort an array using quick sort.
#include<stdio.h>
#include<conio.h>
void main()
int a[100],i,j,n;
clrscr();
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
quicksort(a,0,n-1);
printf("sorted array\n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
getch();
int i,j,p,t;
21 | P a g e
if(low<high)
p=low;
i=low;
j=high;
while(i<j)
while(a[i]<=a[p]&&i<high)
i++;
while(a[j]>a[p])
j--;
if(i<j)
t=a[i];
a[i]=a[j];
a[j]=t;
t=a[p];
a[p]=a[j];
a[j]=t;
quicksort(a,low,j-1);
22 | P a g e
quicksort(a,j+1,high);
}}
OUTPUT:
23 | P a g e
12. Program to sort an array using merge sort.
#include<conio.h>
#include<stdio.h>
void mergesort(int,int);
void merge(int,int,int);
int a[100];
void main()
int i,n;
clrscr();
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
mergesort(0,n-1);
printf("Sorted array\n");
for(i=0;i<n;i++)
printf("\n%d\t\t",a[i]);
getch();
24 | P a g e
{
int mid;
if(low!=high)
mid=(low+high)/2;
mergesort(low,mid);
mergesort(mid+1,high);
merge(low,mid,high);
int b[100];
int h=low;
int j=mid+1;
int i=low;
while((h<=mid)&&(j<=high))
if(a[h]<=a[j])
b[i++]=a[h++];
else
b[i++]=a[j++];
while(h<=mid)
b[i++]=a[h++];
while(j<=high)
25 | P a g e
b[i++]=a[j++];
for(h=low;h<=high;h++)
a[h]=b[h];
OUTPUT:
26 | P a g e
13. Program in C to implement various operations on Circular queue using array
#include<stdio.h>
#include<conio.h>
#define SIZE 5
void enQueue(int);
void deQueue();
void display();
void main()
clrscr();
while(1){
scanf("%d",&choice);
switch(choice){
scanf("%d",&value);
enQueue(value);
break;
case 2: deQueue();
break;
case 3: display();
break;
27 | P a g e
case 4: exit(0);
else{
rear = -1;
cQueue[++rear] = value;
printf("\nInsertion Success!!!\n");
if(front == -1)
front = 0;
void deQueue()
else{
if(front == SIZE)
front = 0;
28 | P a g e
if(front-1 == rear)
void display()
if(front == -1)
else{
int i = front;
printf("%d\t",cQueue[i++]);
else{
printf("%d\t", cQueue[i++]);
i = 0;
printf("%d\t",cQueue[i++]);
29 | P a g e
OUTPUT:
30 | P a g e
14. C Program to implement stack operation using linked list
#include<stdio.h>
#include<conio.h>
void insert();
void delet();
void display();
struct node
{
int d;
struct node *l;
};
struct node *top;
void main()
{
int c;
clrscr();
do{
printf("\nEnter choice:\n1>insert \n2>delete \n3>display \n4>Exit");
scanf("%d",&c);
switch(c)
{
case 1:
insert();
break;
case 2: delet();
break;
case 3: display();
break;
}
}while(c<=3);
getch();
}
void insert()
{
struct node* temp,*p;
temp=(struct node *)malloc(sizeof(struct node));
printf("\nEnter data");
scanf("%d",&temp->d);
temp->l=top;
top=temp;
}
void delet()
{
int i=1;
struct node* temp;
31 | P a g e
temp=top;
if(temp==NULL){
printf("\n The list is empty");
}
else {
printf("Deleted data is %d",temp->d);
top=temp->l;
}
}
void display()
{
struct node* temp;
temp=top;
if(temp==NULL)
{
printf("\n Empty linked list");
}
else{
while(temp!=NULL)
{
printf(" %d ",temp->d);
temp=temp->l;
}
}
}
OUTPUT:
32 | P a g e
15. Program in C to implement various operations on Linear queue using Linked List
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
struct node{
int d;
struct node *l;
};
struct node *f,*r;
void insert();
void delet();
void display();
void main()
{
int c;
clrscr();
do{
printf("\nEnter a choice \n1-->insert \n2-->delete \n3-->display \n4-->Exit");
scanf("%d",&c);
switch(c)
{
case 1: insert();
break;
case 2: delet();
break;
case 3: display();
break;
}
}while(c<=3);
if(c==4)
{
printf("\nDhanyavaad ;) ");
}
if(c>4)
{
printf("\nInvalid Choice ");
}
getch();
}
void insert()
{
struct node* temp;
temp=(struct node *)malloc(sizeof(struct node));
printf("\nEnter data");
scanf("%d",&temp->d);
temp->l=NULL;
33 | P a g e
if(f==NULL)
{
f=r=temp;
}
else{
r->l=temp;
r=temp;
}
}
void delet()
{
struct node* temp;
if(f==NULL)
{
printf("\nList is empty");
}
else{
temp=f;
printf("\nDeleted element is %d ",temp->d);
f=temp->l;
free(temp);
}
}
void display()
{
if(f==NULL)
{
printf("\nList is empty");
}
else{
struct node *temp;
temp=f;
while(temp!=NULL)
{
printf(" %d ",temp->d);
temp=temp->l;
}}}
34 | P a g e
OUTPUT:
35 | P a g e
16. Program to create Binary Search Tree
#include<stdio.h>
#include<stdlib.h>
node *create();
void insert(node *,node *);
void preorder(node *);
int main()
{
char ch;
node *root=NULL,*temp;
do
{
temp=create();
if(root==NULL)
root=temp;
else
insert(root,temp);
node *create()
{
node *temp;
printf("nEnter data:");
temp=(node*)malloc(sizeof(node));
scanf("%d",&temp->data);
temp->left=temp->right=NULL;
return temp;
36 | P a g e
}
if(temp->data>root->data)
{
if(root->right!=NULL)
insert(root->right,temp);
else
root->right=temp;
}
}
37 | P a g e
17 program in C to implement various operations on Circular queue using Linked List.
#include<stdio.h>
#include<conio.h>
struct node
int d;
};
void insert();
void delet();
void display();
void main()
int c;
clrscr();
do{
scanf("%d",&c);
switch(c)
case 1: insert();
break;
case 2: delet();
break;
case 3: display();
38 | P a g e
break;
}while(c<=3);
if(c==4)
if(c>4)
getch();
void insert()
printf("\nEnter data");
scanf("%d",&temp->d);
temp->l=NULL;
if(r==NULL)
r=temp;
temp->l=r;
else
39 | P a g e
{
temp->l=r->l;
r->l=temp;
r=temp;
void delet()
if(r==NULL)
printf("Empty");
else if(r->l==r)
temp=r;
r=NULL;
printf("\nDeleted is %d",temp->d);
free(temp);
else
temp=r->l;
r->l=temp->l;
printf("\nDeleted is %d ",temp->d);
free(temp);
40 | P a g e
}
void display()
if(r==NULL)
printf("\nEmpty");
else{
struct node * q;
q=r->l;
while(q!=r)
printf(" %d ",q->d);
q=q->l;
printf(" %d ",r->d);
41 | P a g e
OUTPUT:
42 | P a g e