-
Notifications
You must be signed in to change notification settings - Fork 0
/
linear_using_userinput.c
38 lines (37 loc) · 1003 Bytes
/
linear_using_userinput.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <stdio.h>
int linearsearch(int *arr, int size, int ele)
{
for (int i = 0; i < size; i++)
{
if (arr[i] == ele)
{
return i;
}
}
return -1;
}
int main()
{
int size;
printf("enter the size of the array :");
scanf("%d", &size);
int arr[size];
printf("enter %d the element of the array:\n", size);
for (int i = 0; i < size; i++)
{
scanf("%d", &arr[i]);
}
int ele;
printf("enter the element to search for: ");
scanf("%d", &ele);
int index = linearsearch(arr, size, ele);
if (index == -1)
{
printf("The element is not present in the array.");
}
else
{
printf("The element is present at array[%d].", index);
}
return 0;
}