Search This Blog

Wednesday 28 October 2015

Program for sequential search


import java.util.Scanner;

public class SeqSearch {
  public static void main(String args[]) {
    Scanner takeIn=new Scanner(System.in);
    int size;
    System.out.print("Enter the number of elements: ");
    size=takeIn.nextInt();
    int a[]=new int[size];
    System.out.println("Enter the elements:");
    for(int i=0;i<size;i++)
      a[i]=takeIn.nextInt();
    System.out.print("Enter the element to be searched: ");
    int x,i=-1;
    x=takeIn.nextInt();
    while(++i<size)
      if(a[i]==x) {
        System.out.println(String.format("Element %d found at location %d",x,i+1));
        break;
      }
    if(i==size)
      System.out.println(String.format("%d not found",x));
  }
}



OUTPUT:

C:\jdk1.7.0\bin>java SeqSearch
Enter the number of elements: 5
Enter the elements:
10 20 30 40 50
Enter the element to be searched: 40

Element 40 found at location 4

1 comment:

  1. Top 3 best slots online in 2021 - DRMCD
    Best 하남 출장샵 Casino Slots Online · 1. Slot Machines · 2. 영주 출장마사지 Playtech Slots · 3. Real Money Slot Machines · 동해 출장샵 4. Bingo Slot Machines · 서울특별 출장안마 5. Live Dealer Casinos. 울산광역 출장마사지

    ReplyDelete