ICSE 2022 SEM-2 QUESTION 2 SOLVED

 /* Define a class to perform binary search on a list of integers given below, to search for an element to input by the user,

if it is found display the element along with is position, otherwise display the message "Search element not found"

2, 5, 7, 10, 15, 20, 29, 30, 46, 50 */

import java.util.*;

class QUESTION_2

{

    public static void main(String[]args)

    {

        Scanner sc=new Scanner(System.in);

        int ar[]={2, 5, 7, 10, 15, 20, 29, 30, 46, 50};

        int lc=0,uc=ar.length,mid=0,p=0,k=0;

        System.out.println("Enter a number to be search:");

        int n=sc.nextInt();

        while(uc>lc)

        {

            mid=(lc+uc)/2;

            if(ar[mid]>n)

            uc=mid-1;

            if(ar[mid]<n)

            lc=mid+1;

            if(ar[mid]==n)

            {

                p=mid;

                k=1;

                break;

            }

        }

        if(k==1)

        {

            System.out.println(n+" is presnt in "+(p+1)+" position");

        }

        else

        {

            System.out.println("Search element not found");

        }

    }

}

Second largest number in java using ternary operator

 //Largest second number using ternary operator public class Main { public static void main(String[] args) { int a=5,b=6,c=7; int ...