Linear Search In Java(program)

import java.io.*;
class LinarySearch
{
public static void main(String args[])
{
try
{
int arr[]=new int[100]; //creating An Array
int i,item,n,s=0;
System.out.println(“———–Linear Search——–“);
DataInputStream dis=new DataInputStream(System.in);
System.out.println(“Enter The Number Of Elements:”);
n=Integer.parseInt(dis.readLine());
System .out.println(“Enter The Element In The Array:”);
for(i=0;i<=n;i++)
{
arr[i]=Integer.parseInt(dis.readLine());
}
System.out.println(“Entered Elements Are:”);
for(i=0;i<=n;i++)
{
System.out.println(arr[i]);
}
System.out.println(“Enter The Element You Want To Search:”);
item=Integer.parseInt(dis.readLine());
for(i=0;i<=n;i++)
{
if(arr[i]==item)
{
System.out.println(“Element Is Found At Location:”+i);
s=1;
break;
}
}
if(s==0)
{
System.out.println(“Element Is Not Found”);
}

}
catch(Exception e) {}
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s