0% found this document useful (0 votes)
2 views2 pages

Java Binary Search Program Example

The document presents a Java program for implementing binary search on an array. It includes code that prompts the user to input numbers, searches for a specified number, and indicates whether the number is found or not. The program utilizes a while loop to perform the search efficiently.

Uploaded by

ashish
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

Java Binary Search Program Example

The document presents a Java program for implementing binary search on an array. It includes code that prompts the user to input numbers, searches for a specified number, and indicates whether the number is found or not. The program utilizes a while loop to perform the search efficiently.

Uploaded by

ashish
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Experiment No.

3
Name:- Swapnil Pandurang Shinde
Roll No:- 2136
 Program on Array.
[Link] Search
Code:-
import [Link].*;
class b_search
{
public static void main(String arg[])
{
int a[]=new int[10];
int n,i,x,end,mid,beg=0,sum=0;
Scanner sc=new Scanner([Link]);
[Link]("Enter how many no.s = ");
n=[Link]();
[Link]("Enter numbers = ");
for(i=0;i<n;i++)
{
a[i]=[Link]();
}
[Link]("Enter number you want to find in array = ");
x=[Link]();
end=n;

while(beg<end)
{
mid=(beg+end)/2;
if(x==a[mid])
{
[Link](a[mid]+" found ");
break;
}
if(x>a[mid])
{
beg=mid+1;
}
if(x<a[mid])
{
end=mid-1;
}
}
if(beg==n)
{
[Link](x+" not found");
}
}
}

Output:-

You might also like