diff --git a/OOP/Java/Lab/Week4/SearchCount.java b/OOP/Java/Lab/Week4/SearchCount.java new file mode 100644 index 0000000..ed29b74 --- /dev/null +++ b/OOP/Java/Lab/Week4/SearchCount.java @@ -0,0 +1,37 @@ +import java.util.Scanner; + +class SearchCount { + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + System.out.println( + "Enter the number of rows and columns in the matrix: " + ); + int m = sc.nextInt(); + int n = sc.nextInt(); + + int[][] matrix = new int[m][n]; + System.out.println("Enter the elements of the matrix:"); + int count = 0; + System.out.println("Enter the element to search for:"); + int searchElement = sc.nextInt(); + + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + int element = sc.nextInt(); + matrix[i][j] = element; + if (element == searchElement) { + count++; + } + } + } + + System.out.println( + "The element " + + searchElement + + " occurs " + + count + + " times in the matrix." + ); + } +}