C Program to Search an input element in the Array

This page has a C Program to Search an element in Array. It is clear that, this C program will search the given input element in the array. For example: If we enter Array elements one by one as 35 78 43 12 46 and the element to be searched is 12 , then the program will display the Location of the searched element as 4. The below given C program will search the given input element in the array. Kindly check out the program to Search an element in Array.

Program to Search an element in Array

#include<stdio.h>

int main(){

int array1[30];

int i, j, n1, n2;

printf("

Enter no of elements to array :");

scanf("%d", &n1);

printf("



Enter the Array elements one by one

");

for (i = 1; i<=n1; i++) {

scanf("%d", &array1[i]);

}

printf("

Enter the element to be searched :");

scanf("%d", &n2);

for (i = 1; i<=n1; i++)

if(array1[i]==n2){

j=i;

}

printf("

The Array elements are :

");

for (i = 1; i<=n1; i++)

printf("

%d", array1[i]);

printf("



The Location of the searched element %d in the Array is %d", n2, j);

return(0);

} Sample Output: Enter no of elements to array :5



Enter the Array elements one by one

35

78

43

12

46



Enter the element to be searched :43



The Array elements are :

35

78

43

12

46



The Location of the searched element 43 in the Array is 3

Related Popular Posts: