# Linear Search program in Java

class operation

{

static int Linear_Search(int K[], int n, int KEY)

{

int i;

for(i=1;i<=n;i++)

if(KEY == K[i])

return(i);

return(-1);

}

}

class searching

{

public static void main(String args[ ])throws IOException

{

int i,n = 0,KEY, flag=0;

String ans=”y”;

DataInputStream in = new DataInputStream(System.in);

System.out.print(“Enter how many numbers to be stored : “);

n = Integer.parseInt(in.readLine());

int K[] = new int[n];

System.out.println(“Enter the number in INCREASING ORDER PLEASE.”);

for(i=0;i<n;i++)

{

K[i] = Integer.parseInt(in.readLine());

}

operation op=new operation();

System.out.println(“SEARCHING BY LINEAR SEARCH”);

System.out.print(“Enter the number to be searched : “);

KEY = Integer.parseInt(in.readLine());

flag=op.Linear_Search(K, n,KEY);

if (flag == -1)

System.out.println(” Number Not present in the given array”);

else

System.out.println(” Number “+KEY+” found at “+(flag+1)+” location in the array”);

}

}

/*OUTPUT:

Enter how many numbers to be stored : 5

Enter the number in INCREASING ORDER PLEASE.

15

25

35

45

55

SEARCHING BY LINEAR SEARCH

Enter the number to be searched : 25

Number 25 found at 2 location in the array

*/

## Leave a Reply

Be the First to Comment!