Class 10th ICSE

Q.6

Write a Java program to initialise an array by the year of graduation from school as an integer value from the user. Using the binary search technique on the sorted array of integers given below. Output the message “Record exists” if the value input is located in the array. If not, output the message “Record does not exist”. { 1982,1987,1993,1996,1999,2003,2006,2007,2009,2010}

Solution :- 

import java.util.*;
class p6{
public static void main(){
Scanner sc =new Scanner(System.in);
int i;
int a[]={1982,1987,1993,1996,1999,2003,2006,2007,2009,2010};
System.out.println(“Enter record you want to search”);
int record=sc.nextInt();
int pos=-1,first=0,last=10-1,mid;
while(first<=last){
mid=(first+last)/2;
if(record==a[mid]){
pos=mid;
break;
}
else if(record>a[mid]){
first=mid+1;
}
else if(record<a[mid]){
last=mid-1;
}
}
if(pos!=-1)
System.out.println(“Record exists “);
else
System.out.println(“Record does not exist”);
}
}

Fun & Easy to follow
Works on all devices
Your own Pace
Super Affordable

Popular Videos

Play Video

UX for Teams

Learn the basics and a bit beyond to improve your backend dev skills.

ava4.png
Chris Matthews

Designer

Play Video

SEO & Instagram

Learn the basics and a bit beyond to improve your backend dev skills.

ava4.png
Chris Matthews

Designer