Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Laboratory file

on
Data Structures Lab

School of Engineering and Technology


Department of Computer Science and Engineering

Subject code – CSP242

SUBMITTED BY: SUBMITTED TO:


Ashima Pandey Ms. Kamakshi Gupta
Roll No.: 210101121 (Assistant Professor)

Sharda University
Greater Noida, Uttar Pradesh
INDEX

S.No. EXPERIMENT Date of the Page Remarks

Experiment No.
24/08/22

Experiment-1
Program 1: Write a program to perform linear search in an array.
Code:
#include <stdio.h>

int main ()

int arr[10];

int i, k, p = -1;

printf ("ENTER THE ELEMENTS: ");

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

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

printf ("ENTER THE ELEMENT TO BE SEARCHED: ");

scanf ("%d", &k);

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

if (arr[i] == k)

p = i;

break; }

if (p == -1)

printf ("ELEMENT NOT FOUND");

else

printf ("ELEMENT FOUND SUCCESSFUL AT POSITION : %d", p + 1);

return 0;

Output:

1
31/08/22

Experiment-2
Program 2: Write a program to perform binary search in an array
Code:
#include <stdio.h>
int main ()
{
int arr[10];
int i,k,p=-1,f=0,l=9,mid;
printf("ENTER THE ELEMENTS: ");
for (i=0;i<10;i++)
{
scanf("%d" ,&arr[i]);
}
printf("ENTER THE ELEMENT TO BE SEARCHED IN INCREASING ORDER :");
scanf("%d",&k);
while(f<=l)
{
mid=(int)(f+l)/2;
if(arr[mid]==k)
{
p=mid;
break;
}
else
{
if (arr[mid]>k)
l=mid-1;
else
f=mid+1;

2
}
}
if(p==-1)
printf("ELEMENT NOT FOUND");
else
printf("ELEMENT FOUND SUCCESSFUL AT POSITION :%d",p+1);

return 0;
}
Output:

3
14/09/22

Experiment-3
Program 1: Write a program to perform array operation: Traversing, Insertion, Deletion,
Modification.
Code:
#include <stdio.h>

int main(){

int i,n,num,c,k;

char res='y';

printf("ENTER THE NUMBER OF ELEMENTS: ");

scanf("%d",&n);

int arr[n];

printf("ENTER THE ELEMENTS: ");

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

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

do{

printf("Press 1 for Traversing \nPress 2 for Insertion \nPress 3 for Deletion\nPress 4 for Modification\n");

printf("Enter your Choice:");

scanf("%d",&c);

switch(c) {

case 1:

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

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

break;

case 2:

printf("Enter the element to be inserted :");

scanf("%d",&k);

arr[n]=k;

printf("Element inserted successfully!\n");

break;

case 3:

printf("Enter the index of the element to be removed :");

scanf("%d",&k);

for(i=k;i<n;i++){

arr[i]=arr[i+1];

4
}

printf("Element removed successfully !\n");

break;

case 4:

printf("Enter the index value to be changed and the number:");

scanf("%d %d",&k,&num);

arr[k]=num;

printf("Modification is Successfully!\n");

break;

printf("TO Continue, Press Y :");

scanf("%s",&res);

while(res=='Y'|| res=='y');

Output:

5
Program 2: Write a program that gives pair of numbers in an array whose sum is greater
than 50.
Code:
#include <stdio.h>

int main(){

int i,n,j;

printf("ENTER THE NUMBER OF ELEMENTS: ");

scanf("%d",&n);

int arr[n];

printf("ENTER THE ELEMENTS: ");

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

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

printf("SUM PAIRS WHOSE SUM IS GREATER THAN 50");

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

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

if((arr[i]+arr[j])>50)

if(i!=j)

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

Output:

6
21/09/22

Experiment-4
Program 1: Write a program to print Fibonacci series.
Code:
#include <stdio.h>

int main()

printf("Enter the number : ");

int n,i,n1,n2,s;

scanf("%d",&n);

n1=0;

n2=1;

printf("%d , %d ",n1,n2);

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

s=n1+n2;

n1=n2;

n2=s;

printf(", %d ",s);

return 0;

Output:

7
Program 2: Write a program to display prime number till n terms.
Code:
#include <stdio.h>

int main ()

printf("Enter the number to be checked :");

int n,i,c=0;

scanf("%d",&n);

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

if(n%i==0)

c++;

if(c==0)

printf("Number is prime");

else

printf("Number is not prime");

return 0;

Output:

You might also like