Page 469 - CA_Blue( J )_Class10
P. 469
Variable Description
NAME DATATYPE DESCRIPTION
i int Loop Variable
name[] String[] Array to store names
marks[] Int[] Array to store marks
n int Size of Array
tempname String Temporary String Variable
tempmarks int Temporary int variable
(c) Programs based on search techniques mentioned in the scope of the syllabus.
Write a program to input a number and search it from the given elements using binary search
Program 11 technique. If it is found display the element along with its position, otherwise display the message
"Search element not found".
The given elements are 6,14, 18, 25, and 30.
1 import java.util.*; // Importing package "util"
2 class binarysearch // Class declaration
3 {
4 public static void main()
5 {
6 Scanner sc = new Scanner(System.in);
7 int arr[] = {6,14, 18 , 25,30}; // Given elements
8 int n,min=0,max=4,mid,pos=-1; //Declaration of variables
9 System.out.print("Enter number to search: ");
10 n= sc.nextInt();
11 while (min <= max) //Binary search technique
12 {
13 mid = (min+max) / 2;
14 if (arr[mid] < n)
15 min = mid + 1;
16 else if (arr[mid] > n)
17 max = mid - 1;
18 else
19 {
20 pos = mid;
21 break;
22 }
467
Internal Assessment 467

