ICSE Computer Science Java Programs | IT Developer
IT Developer

Java Programs - Solved 2014 ICSE Computer Science Paper



Share with a Friend

Solved 2014 ICSE Computer Science Paper

Class 10 - ICSE Computer Science Solved Papers

Array Sort - Binary Search Program - ICSE 2014 Computer Science

Write a program to accept 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}

import java.util.Scanner; class BinarySearch{ public static void main(String[] args){ Scanner in = new Scanner(System.in); int y[] = {1982, 1987, 1993, 1996, 1999, 2003, 2006, 2007, 2009, 2010}; System.out.print("Year of graduation: "); int key = Integer.parseInt(in.nextLine()); int low = 0; int high = y.length - 1; int mid = 0; while(low <= high){ mid = (low + high) / 2; if(key == y[mid]) break; else if(key < y[mid]) high = mid - 1; else low = mid + 1; } if(low <= high) System.out.println("Record exists"); else System.out.println("Record does not exist"); } }

Output

 
 OUTPUT 1: 
Year of graduation: 2006
Record exists 

 OUTPUT 2: 
Year of graduation: 2005
Record does not exist