m to search for an integer value input by the user in the sorted list given below using binary search technique
Write a program to search for an integer value input by the user in the sorted list given below using
binary search technique. If found display “Search Successful”, otherwise display “Search
Unsuccessful”.
{31, 36, 45, 50, 60, 75, 86, 90}
import java.util.*;
class BinarySearch
{
public static void main(String args[ ])
{
Scanner sc = new Scanner(System.in);
int array[ ] = {31, 36, 45, 50, 60, 75, 86, 90};
int l=0, u=7, m=0;
boolean found = false;
System.out.print("Enter a number ");
int num = sc.nextInt( );
while(l <= u)
{
m = (l+u) / 2;
if(num == array[m])
found = true;}
if(num > array[m])
l = m + 1;
else
u = m - 1;
if(found == true)
System.out.println("Search Successful");
else
System.out.println("Search Unsuccessful");
}
}
FOR CODE ,
CLICK : https://onecompiler.com/java/3y4fgdx4a (TO FIND OUTPUT)