Add OOP/Java/Lab/Week4/SearchCount.java

This commit is contained in:
Aadit Agrawal 2024-08-31 02:05:55 +05:30
parent 3acb98a896
commit 46fa59b5f4

View File

@ -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."
);
}
}