Linear Search program in java

 Q. write a program in java to perform a linear search.

Ans:-

import java.io.*;

public class LnearSearch

{

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

{

InputStreamReader in=new InputStreamReader(System.in);

BufferedReader br=new BufferedReader(in);

int i,s,n,h=0 ;

System.out.println("Enter length of Array=");

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

int A[ ]=new int[n];

System.out.println("Enter arrays elemnts =");

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

{

A[i]=Integer.parseInt(br.readLine());

}

System.out.println("Arrays elemnts are =");

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

{

System.out.println(A[i]);

}

System.out.println("Enter elemnt which to be searched=");

s=Integer.parseInt(br.readLine());

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

{

if(s==A[i])

{

System.out.println("elements is found");

h=1;

}

}

if(h==0)

{

System.out.println("Element is not found");

}

}

}


                            if you run in blue-j application then this program is looking as-






OUTPUT HERE 


if the element is not found then the output will be 



Comments

Popular posts from this blog

HTML

Administration of the Internet

FUNDAMENTAL OF COMPUTER