-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpractical17.java
More file actions
64 lines (57 loc) · 2.02 KB
/
practical17.java
File metadata and controls
64 lines (57 loc) · 2.02 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
53
54
55
56
57
58
59
60
61
62
63
64
//Write a program that generate 6*6 two dimensional matrix, filled with 0’s and 1’s , display the matrix, check every raw and column have an odd number’s of 1’s.
import java.util.Random;
public class practical17 {
public static void main(String[] args) {
int[][] matrix = new int[6][6];
Random rand = new Random();
// Fill the matrix with random 0's and 1's
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 6; j++) {
matrix[i][j] = rand.nextInt(2); // Generates either 0 or 1
}
}
// Display the matrix
System.out.println("Generated Matrix:");
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 6; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
// Check rows and columns for odd number of 1's
boolean valid = true;
// Check rows
for (int i = 0; i < 6; i++) {
int countOnes = 0;
for (int j = 0; j < 6; j++) {
if (matrix[i][j] == 1) {
countOnes++;
}
}
if (countOnes % 2 == 0) { // Even number of 1's
valid = false;
break;
}
}
// Check columns if rows are valid
if (valid) {
for (int j = 0; j < 6; j++) {
int countOnes = 0;
for (int i = 0; i < 6; i++) {
if (matrix[i][j] == 1) {
countOnes++;
}
}
if (countOnes % 2 == 0) { // Even number of 1's
valid = false;
break;
}
}
}
if (valid) {
System.out.println("Every row and column has an odd number of 1's.");
} else {
System.out.println("Not every row and column has an odd number of 1's.");
}
}
}