Computer Science, asked by prap444pdejk6, 1 year ago

Pls Write an Array proram In linear search with taking inputs from the user

Answers

Answered by sswaraj04
1

Answer:

import java.util.Scanner;

class LinearSearch

{

  public static void main(String args[])

  {

     int n, item;

     Scanner sc = new Scanner(System.in);

     System.out.println("Enter number of elements:");

     n = sc.nextInt();

     int[] array = new int[n];

     System.out.println("Enter " + n + " integers");

     

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

       array[i] = sc.nextInt();

     System.out.println("Enter the search value:");

     item = sc.nextInt();

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

     {

        if (array[i] == item)

        {

          System.out.println(item+" is present at location "+(i+1));

          break;

        }

     }

     if (i== num)

       System.out.println(item + " doesn't exist in array.");

  }

}

Explanation:

Logic is very simple:

   Start from the leftmost element of array[] and one by one compare x with  each element of array[]

   If x matches with an element, print the index and break the loop

   If x doesn’t match with any of elements, i equals value of number of elements then it prints the message it doen't exist in array


sswaraj04: do you want explanation or not??
prap444pdejk6: What Happened Bro
prap444pdejk6: Bro wat happened
prap444pdejk6: Are u typing the program bro
prap444pdejk6: Write an Array proram In Binary search with taking inputs from the user!!!Bro Pls tell me this answer which i posted as a question in public
prap444pdejk6: I hope and Believe that u will answer BRO!!!
sswaraj04: wait
sswaraj04: do you really need to take input from users
sswaraj04: wait then it makes it more complex
prap444pdejk6: yes i wanna take input bro
Similar questions
Math, 6 months ago