Q:

C Program to find a number from array elements

0

C Program to find a number from array elements

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include<stdio.h>
#define MAX 20 
 

 
void    readArray(int a[],int size) 
{ 
    int i; 
    for(i=0;i< size;i++) 
    { 
        printf("Enter %d element :",i+1); 
        scanf("%d",&a[i]); 
    } 
} 
 

int findElement(int a[],int size,int item) 
{ 
    int i,pos=-1; 
    for(i=0;i< size;i++) 
    { 
        if(a[i]==item) 
        { 
            pos=i; 
            break; 
        } 
    } 
    return pos; 
} 
  
int main() 
{ 
    int arr[MAX]; 
    int n,item,pos; 
 
 
    printf("\nEnter size of an Array :"); 
    scanf("%d",&n); 
 
    printf("\nEnter elements of Array 1:\n"); 
    readArray(arr,n); 
      
    printf("Enter an item to find :"); 
    scanf("%d",&item); 
      
    pos=findElement(arr,n,item); 
    if(pos==-1) 
        printf("\n%d does not exists in array.\n",item); 
    else
        printf("\n%d find @ %d position.\n",item,pos);   
      
 
    printf("\n\n"); 
    return 0; 
} 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now