Untitled
unknown
plain_text
2 years ago
1.1 kB
6
Indexable
try catch public class Main{ public static void main(String s[]){ int[] num = {1,2,3}; try{ System.out.println(num[10]); }catch(Exception e){ System.out.println("Error Found, recheck your program."); }finally{ System.out.println("The 'Try Catch' is finished."); } } } binary import java.util.Scanner; public class BS { public static void binarySearch(int arr[],int start,int end,int key) { int mid=(start+end)/2; while(start<=end) { if(arr[mid]<key) { start=mid+1; } else if(arr[mid]==key) { System.out.println("element is found at index:"+mid); break; } else { end=mid-1; } mid=(start+end)/2; } if(start>end) { System.out.println("element is not found"); } } public static void main(String[]args) { Scanner obj=new Scanner(System.in); int arr[]=new int[5]; for(int i=0;i<5;i++) { arr[i]=obj.nextInt(); } System.out.println("enter the key "); int key=obj.nextInt(); int end=arr.length-1; binarySearch(arr,0,end,key); } }
Editor is loading...
Leave a Comment