Linear Search :Java Program

package javaPrograms;

import java.util.Scanner;


public class LinerSearch
{


static int n;


public static void main(String a[])
{


Scanner sc=new Scanner(System.in);
System.out .println("Enter the size of array");

int size=sc.nextInt();


int[] arr=new int[size];
System.out .println("Enter the values of array");

for(int i=0; i<size; i++)
 {
arr[i]=sc.nextInt();

 }

      System.out.println("Enter no.  to Search");
 int noSearch =sc.nextInt();
     boolean vFound=false;
     for(int i=0; i<size; i++)
      {
        if(noSearch==arr[i])
        {
System.out.println("value is found="+noSearch);
           vFound=true;
           break;
         }
    }

 if(!vFound)
 {
 System.out.println("value not found");
 }


}
}
Output
------------------------------------------------------------------------------------------------------------------------------
Enter the size of array
3
Enter the values of array

2
3
4
Enter no.  to Search
2
value is found=2


Post a Comment

0 Comments