Monday, 27 October 2014

Linear Search

Non-Recursive C Program:

Aim:
Linear Search program

Program:
#include<stdio.h>
#include<conio.h>
void main ()
{
   int A[25],i,num,flag=0,m;

printf("Enter the size of array:");
scant("%d",&m);

printf("\n Enter the numbers:");
  for(i=0;i<m;i++)
    {
       scanf("%d",&A[i]);
    }

printf("\n The Elements of array:");
for(i=0;i<m;i++)
    {
       printf("%d\t",A[i]);
    }

printf("\n Enter the number to be search:");
scant("%d",&num);

for(i=0;i<m;i++)
    {
       if(num == A[i])
         {
            flag=1;
            break;
          }
    }
   if( flag==1)
printf("\n Search successful, the number %d is found at %d position ",num,i+1);
    else
printf("\nSearch unsuccessful,the number %d is not found ",num);
}

Output:
Enter the size of array : 10
Enter the numbers:
19
21
32
43
54
65
76
87
98
100
Enter the number to be search: 87
The number 87 found at 8 position.

No comments:

Post a Comment