Java Linear Search Program

Java linear search program is very easy.To check the element in the list which we want to search ,we compare with the every element of the list of element.If element found which we want to search then find the location and print on the screen otherwise simply show the message.

Java Linear Program

import java.util.Scanner;
public class LinearSearchProgram
{
public static void main(String[] arg)
{
int n,search_element,i,array[];
System.out.println("Enter the number of element");
Scanner in=new Scanner(System.in);//Scanner is used to take input 
n=in.nextInt();
array=new int[n];

System.out.println("Enter the" +n+"Integer");

for(i=0;i<n;i++)
{
array[i]=in.nextInt();
} 
System.out.println("Enter the element which you want to search");
search_element=in.nextInt();

for(i=0;i<n;i++)
{
if(array[i]==search_element)//comparing the element  
{
System.out.println(Search_element+ "found at the location" +(i+1)+".");
break;
}
}
if(i==n)
{
System.out.println("Element is not present in the list of element");
}
}
}

Output:

Enter the number of element
5
Enter the 5 Integer
8
9
6
3
1
Enter the element which you want to search
6
6 found at the location 3.


The time complexity of linear search is O(n).

sumankumar80

Hello Friends,I have written and developed this website because you can learn technology easily ,prepare for the placement and also for the any type of examination . By :Suman Kumar

2 thoughts on “Java Linear Search Program

  • February 19, 2019 at 7:27 am
    Permalink

    it is good web application and help full platform really i like ed

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *