MIT-Curricular/DS/C/Lab/Week1/linear.c

44 lines
862 B
C
Raw Normal View History

2024-08-05 01:08:32 +05:30
#include <stdio.h>
int main()
{
int a,n,i;
int flag=0;
int arr[10];
printf("Enter the number of elements you want in the array: ");
scanf("%d",&n);
printf("Enter the numbers you want in the array:\n");
for(i=0;i<n;i++){
scanf("%d",&arr[i]);
}
printf("The numbers in the array are:\n");
for(i=0;i<n;i++){
printf("%d\n",arr[i]);
}
printf("Enter the number you want to search: ");
scanf("%d",&a);
//linear search
for(i=0;i<n;i++){
if(arr[i]==a){
flag=1;
printf("\nThe desired element is at position %d.\n",i+1);
break;
}
}
if(flag){
printf("The number has been found.\n");
}else{
printf("The number does not exist in this array.\n");
}
return 0;
}