C Program to Find an Element from Array
Write C Program to Find an Element from Array
// C Program to Find an Element from Array
#include <stdio.h>
int main()
{
int A[] = {1, 2, 3, 4, 5}, n, i, flag=0;
printf("The Array :--> ");
for(i=0; i<5; i++)
{
printf("%d ", A[i]);
}
printf("\nEnter Element to Search from Array :--> ");
scanf("%d", &n);
for(i = 0; i < 5; i++)
{
if(A[i] == n)
{
printf("\nThe element is present at index %d",i);
flag = 1;
break;
}
}
if(flag == 0)
{
printf("\nThe element is not present in the array");
}
return 0;
}
Output:
Run 1:
The Array :--> 1 2 3 4 5
Enter Element to Search from Array :--> 2
The element is present at index 1
Run 2:
The Array :--> 1 2 3 4 5
Enter Element to Search from Array :--> 22
The element is not present in the array