LATEST PROGRAMS

WAP in C to search an element in an array and display its position.

#include<stdio.h>
#define MAX 100

void main()
{
    int arr[MAX];
    int i, size, ele, flag=0;
    printf("Enter number of elements of the array:\n");
    scanf("%d",&size);
    printf("Enter the elements of the array:\n");
    for(int i=0;i<size;i++)
    {
        scanf("%d",&arr[i]);
    }
    printf("Enter the element to be searched:\n");
    scanf("%d",&ele);
    for(i=0;i<size;i++)
    {
        if(arr[i]==ele)
        {
            flag=1;
            break;
        }
    }
    if(flag==1)
    {
        printf("Element %d found in position %d",ele,i+1);
    }
    else
    {
        printf("Element Not found");
    }
}
18/02/2023, 12:33 pm Read : 256 times