11#include <stdio.h>
22
3- void main ()
3+ int main ()
44{
5- int array [10 ];
6- int i , num , keynum , found = 0 ;
5+ int array [100 ], search , c , n ;
76
8- printf ("Enter the value of num \n" );
9- scanf ("%d" , & num );
10- printf ("Enter the elements one by one \n" );
11- for (i = 0 ; i < num ; i ++ )
12- {
13- scanf ("%d" , & array [i ]);
14- }
15- printf ("Input array is \n" );
16- for (i = 0 ; i < num ; i ++ )
17- {
18- printf ("%dn" , array [i ]);
19- }
20- printf ("Enter the element to be searched \n" );
21- scanf ("%d" , & keynum );
22- /* Linear search begins */
23- for (i = 0 ; i < num ; i ++ )
24- {
25- if (keynum == array [i ] )
26- {
27- found = 1 ;
28- break ;
29- }
30- }
31- if (found == 1 )
32- printf ("Element is present in the array\n" );
33- else
34- printf ("Element is not present in the array\n" );
35- }
7+ printf ("Enter the number of elements in array\n" );
8+ scanf ("%d" ,& n );
9+
10+ printf ("Enter %d integer(s)\n" , n );
11+
12+ for (c = 0 ; c < n ; c ++ )
13+ scanf ("%d" , & array [c ]);
14+
15+ printf ("Enter the number to search\n" );
16+ scanf ("%d" , & search );
17+
18+ for (c = 0 ; c < n ; c ++ )
19+ {
20+ if (array [c ] == search ) /* if required element found */
21+ {
22+ printf ("%d is present at location %d.\n" , search , c + 1 );
23+ break ;
24+ }
25+ }
26+ if (c == n )
27+ printf ("%d is not present in array.\n" , search );
28+
29+ return 0 ;
30+ }
0 commit comments