In this blog we will learn how to search for an element in an array. Output is found or Missing on whether the element is found or not. Max. value of array is 100.
Input:
First line consists of integer n which is number of elements in an array.
Second line consists of n elements.
Third line consists of the element to be searched.
3
123
6
Output:
Missing
Input 2:
4
1 2 4 8
2
Output:
Found
Implementation using C:
#include<stdio.h>
#define MAX_SIZE 100
int main()
{
int n, i, array[MAX_SIZE], x;
scanf("%d", &n);
for(i = 0; i < n; i++)
scanf("%d", &array[i]);
scanf("%d", &x);
for(i = 0; i < n; i++)
{
if(x == array[i])
{
printf("Found \n");
return 0;
}
}
printf("Missing \n");
return 0;
}
Happy Coding!
Follow us on Instagram @programmersdoor
Join us on Telegram @programmersdoor
Please write comments if you find any bug in above code/algorithm, or find other ways to solve the same problem.
Follow Programmers Door for more.
Commentaires