Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
1.1 kB
3
Indexable
Never
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);
}
}
Leave a Comment