-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArray2D2.java
More file actions
52 lines (35 loc) · 1.22 KB
/
Array2D2.java
File metadata and controls
52 lines (35 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// This program takes a 3x3 matrix input from the user,
// prints the matrix, and searches for a given key element
// in the matrix and displays its position if found.
import java.util.Scanner;
public class Array2D2 {
public static boolean Search(int matrix[][], int key){
for (int i = 0; i < matrix.length; i++) {
for(int j = 0; j < matrix[0].length; j++){
if(matrix[i][j] == key){
System.out.println("Found at cell (" + i + "," + j + ")");
return true;
}
}
}
System.out.println("Key is not found");
return false;
}
public static void main(String args[]){
int matrix[][] = new int[3][3];
Scanner sc = new Scanner(System.in);
System.out.println("Enter Matrix");
for (int[] matrix1 : matrix) {
for (int j = 0; j < matrix[0].length; j++) {
matrix1[j] = sc.nextInt();
}
}
for (int[] matrix1 : matrix) {
for (int j = 0; j < matrix[0].length; j++) {
System.out.print(matrix1[j] + " ");
}
System.out.println();
}
Search(matrix,5);
}
}