Write a Program in C to Search an Element in the Array using Linear Search (Iteration)
#include<stdio.h>#include<conio.h>
void main()
{
int a[10],i,num,flag=0;
clrscr();
printf("Enter 5 numbers\n");
for(i=0;i<=4;i++)
{
scanf("%d", &a[i]);
}
printf("\nEnter the number to be searched\n");
scanf("%d",&num);
for(i=0;i<=4;i++)
{
if(a[i]==num)
{
flag=1;
printf("Number (%d) found at position (%d)\n",num,i+1);
break;
}
}
if(flag==0)
{
printf("\nDidnot find the number (%d)\n",num);
}
getch();
}
No comments:
Post a Comment