Program to search for a given number in an array using C


/*WAP to search for a given number in an array*/
#include<stdio.h>
#include<stdlib.h>
void main()
{
int a[20],i,n,s;
clrscr();
printf("Input the size of an array-\n");
scanf("%d",&n);
printf("Enter %d elements-\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Input the number to search-\n");
scanf("%d",&s);
for(i=0;i<n;i++)
 if(a[i]==s)
 {
 printf("Number %d found at %d position\n",s,i+1);
 exit(0);
 }
printf("Number %d is not found\n",s);
}

Output

Input the size of an array-
6
Enter 6 elements-
1 5 3 4 7 9
Input the number to search-
4
Number 4 found at 4 position


Input the size of an array-
5
Enter 5 elements-
5 4 1 2 9
Input the number to search-
3
Number 3 is not found

Download Program

Download