program 1
import java.util.*;
public class MyClass {
public static void main(String args[]) {
Scanner in = new Scanner (System.in);
int n, ns, lb =0, ub =9, p=0,k=0;
int m[] = new int [10];
System.out.println("Enter the array");
for (int i =0; i<10; i++){
m[i] = in.nextInt();
}
System.out.println("enter number to be searched");
ns = in.nextInt();
while (lb<ub){
p = (lb+ub)/2;
if (m[p]<ns)
lb = p+1;
if (m[p]>ns)
ub = p-1;
if (m[p] == ns){
k =1;
break;
}
}
if (k==1)
System.out.println("Search successful");
else
System.out.println("Not");
}
}
Comments
Post a Comment