Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Linear Search */
- #include <stdio.h>
- int linear_search(int num[], int n, int value)
- {
- int position = -1;
- for (i = 0; i < n; i++)
- {
- if (value == num[i])
- {
- position = i + 1;
- break;
- }
- }
- return -1;
- }
- int main()
- {
- int num[20];
- int n, i;
- printf("How many numbers? ");
- scanf("%d", &n);
- printf("Enter %d numbers : ", n);
- for (i = 0; i < n; i++)
- {
- scanf("%d", &num[i]);
- }
- int value, position = -1;
- printf("Enter the value you want to search : ");
- scanf("%d", &value);
- int result = linear_search(num, n, value);
- if (result == -1)
- printf("Value is not found\n");
- else
- printf("Value is found at position %d", result );
- }
- /*
- Output:
- How many numbers ? 5
- Enter 5 numbers : 12 23 34 45 56
- Enter the value you want to search : 56
- Value is found at position 5
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement