Computer Science, asked by Anonymous, 6 months ago

WAP in JAVA to accept 10 numbers in an array and check if the entered number to be searched number is present in that array or not

Answers

Answered by BrainlyProgrammer
3

Question:-

  • WAP in JAVA to accept 10 numbers in an array and check if the entered number to be searched number is present in that array or not

Answer:-

package Coder;

import java.util.*;

class Code

{

public static void main (String ar [])

{

/*

*Code written by

*@swetank232894

*/

Scanner sc=new Scanner (System.in);

int a[]=new int[10];

System.out.println("Enter ten elements in an array");

int f=0;

for(int I=0;I<10;I++)

{

a[I]=sc.nextInt();

}

System.out.println("Enter number to be searched");

int n=sc.nextInt();

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

{

if (n==a[i])

{

f=1;

break;

}

}

if(f==1)

System.out.println("Number found");

else

System.out.println("Number not found");

}

}

Variable Description:-

  • a:- To store ten numbers in an array
  • i:- loop variable
  • f:- flag variable to come out of the loop once the number is found
  • n:- to accept the number to be searched from the user

•Output Attached.

Attachments:
Answered by Oreki
4

Using Linear Search technique -

import java.util.Scanner;  

public class SearchingElement_Linear {

   static int linearSearch(int[ ] array, int search) {

       for (int i = 0; i < array.length; i++)

           if (array[i] == search)

               return i;

       return -1;

   }

   public static void main(String[ ] args) {

       Scanner sc = new Scanner(System.in);

       // Accepting elements into the Array.

       System.out.println("Enter the Array elements - ");

       int[ ] array = new int[10];

       for (int i = 0; i < 10; )

           array[i++] = sc.nextInt( );

       // Accepting the search element.

       System.out.print("Enter element to be searched - ");

       int searchElement = sc.nextInt( );

       // Searching for the element if present or not.

       boolean isPresent = linearSearch(array, searchElement) != -1;

       System.out.printf("%s Present", isPresent ? "Is" : "Not");

   }

}

Using Binary Search technique -

import java.util.Arrays;

import java.util.Scanner;

public class SearchingElement_Binary {

   static int binarySearch(int[ ] array, int search) {

       int start = 0, stop = array.length - 1;

       while (start <= stop) {

           int middle = start + (stop - start) / 2;

           if (array[middle] == search)

               return middle;

           if (array[middle] < search)

               start = middle + 1;

           else stop = middle - 1;

       }

       return -1;

   }

   public static void main(String[ ] args) {

       Scanner sc = new Scanner(System.in);

       // Accepting elements into the Array.

       System.out.println("Enter the Array elements - ");

       int[ ] array = new int[10];

       for (int i = 0; i < 10; )

           array[i++] = sc.nextInt( );

       // Accepting the search element.

       System.out.print("Enter element to be searched - ");

       int searchElement = sc.nextInt( );

       // Sorting the Array.

       Arrays.sort(array);

       // Searching for the element if present or not.

       boolean isPresent = binarySearch(array, searchElement) != -1;

       System.out.printf("%s Present", isPresent ? "Is" : "Not");

   }

}

Sample I/O -

Case 1 :

   Enter the Array elements -  

   3 4 2 1 5 7 6 8 9 10

   Enter element to be searched - 4

   Is Present

Case 2 :

   Enter the Array elements -  

   1 2 3 4 6 7 8 9 10 11

   Enter element to be searched - 5

   Not Present

Similar questions