JAVA_CODINGS Telegram 76
56. Matrix Operations.

import java.util.Scanner;

public class Matrices {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter number of rows: ");
int rows = scanner.nextInt();

System.out.print("Enter number of columns: ");
int columns = scanner.nextInt();

System.out.println();
System.out.println("Enter first matrix");
int[][] a = readMatrix(rows, columns);

System.out.println();
System.out.println("Enter second matrix");
int[][] b = readMatrix(rows, columns);

int[][] sum = add(a, b);
int[][] difference1 = subtract(a, b);
int[][] difference2 = subtract(b, a);

System.out.println();
System.out.println("A + B =");
printMatrix(sum);

System.out.println();
System.out.println("A - B =");
printMatrix(difference1);

System.out.println();
System.out.println("B - A =");
printMatrix(difference2);
}

public static int[][] readMatrix(int rows, int columns) {

int[][] result = new int[rows][columns];
Scanner s = new Scanner(System.in);

for (int i = 0; i < rows; i++) {

for (int j = 0; j < columns; j++) {

result[i][j] = s.nextInt();

}
}
return result;
}

public static int[][] add(int[][] a, int[][] b) {

int rows = a.length;
int columns = a[0].length;
int[][] result = new int[rows][columns];

for (int i = 0; i < rows; i++) {

for (int j = 0; j < columns; j++) {

result[i][j] = a[i][j] + b[i][j];

}
}
return result;
}

public static int[][] subtract(int[][] a, int[][] b) {

int rows = a.length;
int columns = a[0].length;
int[][] result = new int[rows][columns];

for (int i = 0; i < rows; i++) {

for (int j = 0; j < columns; j++) {

result[i][j] = a[i][j] - b[i][j];

}
}
return result;
}

public static void printMatrix(int[][] matrix) {

int rows = matrix.length;
int columns = matrix[0].length;

for (int i = 0; i < rows; i++) {

for (int j = 0; j < columns; j++) {

System.out.print(matrix[i][j] + " ");

}
System.out.println();
}
}
}

@java_codings



tgoop.com/java_codings/76
Create:
Last Update:

56. Matrix Operations.

import java.util.Scanner;

public class Matrices {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter number of rows: ");
int rows = scanner.nextInt();

System.out.print("Enter number of columns: ");
int columns = scanner.nextInt();

System.out.println();
System.out.println("Enter first matrix");
int[][] a = readMatrix(rows, columns);

System.out.println();
System.out.println("Enter second matrix");
int[][] b = readMatrix(rows, columns);

int[][] sum = add(a, b);
int[][] difference1 = subtract(a, b);
int[][] difference2 = subtract(b, a);

System.out.println();
System.out.println("A + B =");
printMatrix(sum);

System.out.println();
System.out.println("A - B =");
printMatrix(difference1);

System.out.println();
System.out.println("B - A =");
printMatrix(difference2);
}

public static int[][] readMatrix(int rows, int columns) {

int[][] result = new int[rows][columns];
Scanner s = new Scanner(System.in);

for (int i = 0; i < rows; i++) {

for (int j = 0; j < columns; j++) {

result[i][j] = s.nextInt();

}
}
return result;
}

public static int[][] add(int[][] a, int[][] b) {

int rows = a.length;
int columns = a[0].length;
int[][] result = new int[rows][columns];

for (int i = 0; i < rows; i++) {

for (int j = 0; j < columns; j++) {

result[i][j] = a[i][j] + b[i][j];

}
}
return result;
}

public static int[][] subtract(int[][] a, int[][] b) {

int rows = a.length;
int columns = a[0].length;
int[][] result = new int[rows][columns];

for (int i = 0; i < rows; i++) {

for (int j = 0; j < columns; j++) {

result[i][j] = a[i][j] - b[i][j];

}
}
return result;
}

public static void printMatrix(int[][] matrix) {

int rows = matrix.length;
int columns = matrix[0].length;

for (int i = 0; i < rows; i++) {

for (int j = 0; j < columns; j++) {

System.out.print(matrix[i][j] + " ");

}
System.out.println();
}
}
}

@java_codings

BY Advance Java πŸ‘¨β€πŸ’»


Share with your friend now:
tgoop.com/java_codings/76

View MORE
Open in Telegram


Telegram News

Date: |

Read now On June 7, Perekopsky met with Brazilian President Jair Bolsonaro, an avid user of the platform. According to the firm's VP, the main subject of the meeting was "freedom of expression." Some Telegram Channels content management tips Telegram desktop app: In the upper left corner, click the Menu icon (the one with three lines). Select β€œNew Channel” from the drop-down menu. During the meeting with TSE Minister Edson Fachin, Perekopsky also mentioned the TSE channel on the platform as one of the firm's key success stories. Launched as part of the company's commitments to tackle the spread of fake news in Brazil, the verified channel has attracted more than 184,000 members in less than a month.
from us


Telegram Advance Java πŸ‘¨β€πŸ’»
FROM American