Write a java program to print binary search program using while loop

Write a java program to print binary search program using while loop. In the Java programming language, a binary search is performed. This software is a step up from  the beginner’s java program. In Java, a binary search program can look for an element in an  array and print the value.

public class Main
{ 
public static void binarySearch(int arr[], int a, int b, int key)
{ 
int mid = (a + b)/2; 
while( a <= b ){ 
if ( arr[mid] < key ){ 
a = mid + 1; 
}else if ( arr[mid] == key ){ 
System.out.println("number is here " + mid); 
break; 
}else{ 
b = mid - 1; 
} 
mid = (a + b)/2; 
} 
if ( a > b ){ 
System.out.println("number not here"); 
} 
} 
public static void main(String args[]){ 
int arr[] = {10,20,30,40,50}; 
int key = 30; 
int b=arr.length-1; 
binarySearch(arr,0,b,key); 
} 
} 

Output:

number is here 2