Lab Assignments_05
Lab Assignments_05
Enrollment no:-0801IT231043
B.Tech IInd year, Batch A2
Lab Assignments_05
Que1) Write a program the desired element from the given data using linear
search.
● Program:-
#include <stdio.h>
#include <stdlib.h>
int main(){
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
int* arr=(int*)malloc(n*sizeof(int));
printf("Enter %d elements:\n",n);
for(int i=0;i<n;i++){
scanf("%d", &arr[i]);
}
int result=linearSearch(arr,n,target);
if(result!=-1){
printf("Element found at index %d\n", result);
}
else{
printf("Element not found\n");
}
free(arr);
return 0;
}
● Output:-
Name:-Devansh Singh Nikam
Enrollment no:-0801IT231043
B.Tech IInd year, Batch A2
Que2) Write a program the desired element from the given data using binary
search.
● Program:-
#include <stdio.h>
#include <stdlib.h>
int main() {
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
int target;
printf("Enter the target element: ");
scanf("%d", &target);
free(arr);
return 0;
}
● Output:-