Array Programs
Array Programs
import java.util.*;
class Binary_Searching {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the no of terms:");
int size = sc.nextInt();
int num[] = new int[size];
System.out.println("Enter the values");
for (int i = 0; i < size; i++) {
num[i] = sc.nextInt();
}
System.out.println("Enter the key or value you want to search:");
int x = sc.nextInt();
sc.close();
int first = 0;
int last = num.length - 1;
int mid = (first + last) / 2;
while (first <= last) {
if (num[mid] < x) {
first = mid + 1;
} else if (num[mid] == x) {
System.out.println("Element is found at " + mid);
break;
} else {
last = mid - 1;
}
mid = (first + last) / 2;
}
if (first > last) {
System.out.println("Element is not present");
}
}
}
int[][] jA = { { 1, 2, 3 }, { 4, 5 }, { 6, 7, 8, 9 } };
for (int i = 0; i < jA.length; i++) {
for (int j = 0; j < jA[i].length; j++) {
System.out.print(jA[i][j] + " ");
}
System.out.println();
}
}
}