C Program to Search an Element in an Array

Through this program, the problem of how to find an element from an array has been solved. First an Array is declared in this program, then input is taken from the user inside the Array. After that the value to be searched is initialized. And the value of search value is taken from the user. Then the search value is matched with each element of the Array, if the value is found then the result will be displayed,


#include<stdio.h>
void main(){
    int arr[5];
    printf("Enter Array Elemnts: \n");
    for(int i=0;i<5;i++){
        scanf("%d",&arr[i]);
    }

    int x;
    printf("Please Enter X value; \n");
    scanf("%d",&x);

    int flag=0;

    for(int i=0;i<5;i++){
       if(x==arr[i]){
        printf("the x value is %d and index number is %d",arr[i], i);
        flag++;
        break;

       }
    }

    if(flag==0){
        printf("The x value is not found");
    }
}


Output

Enter Array Elemnts:
1
2
3
4
5
Please Enter X value;
4
the x value is 4 and index number is 3

Post a Comment

Previous Post Next Post