Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class EvenNumberRowsAndColumns {
- public static void main(String[] args) {
- //create matrix and fill with 0 and 1
- int[][] matrix= new int[6][6];
- boolean even = true;
- // fill with randoms 0's and 1's
- for (int i=0; i<matrix.length; i++) {
- for (int j=0; j<matrix[i].length; j++) {
- matrix[i][j] = (int)(Math.random() * 2);
- }
- }
- // check for even rows
- for (int i=0; i<matrix.length; i++) {
- int rowSum=0;
- for (int j=0; j<matrix[i].length; j++) {
- // at this point I need a true or false
- // so I'm going back up and making one
- rowSum+= matrix[i][j];
- }
- if (rowSum % 2 != 0) {
- even = false;
- }
- }
- // check for even columns
- for (int j=0; j<matrix[0].length; j++) {
- int colSum=0;
- for (int i=0; i<matrix.length; i++) {
- colSum+= matrix[i][j];
- }
- if (colSum % 2 !=0) {
- even = false;
- }
- }
- // print matrix and print result
- for (int i=0; i<matrix.length; i++) {
- for (int j=0; j<matrix[i].length; j++) {
- System.out.print(matrix[i][j]+ " ");
- }
- System.out.println();
- }
- System.out.println(even);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement