Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Matrix {
- public static void main (String[] args){
- Scanner sc = new Scanner(System.in);
- System.out.println("Enter the number of rows for the 1st matrix (Matrix A): ");
- int s = sc.nextInt();
- System.out.println("Enter the number of columns for the 1st matrix (Matrix A): ");
- int t = sc.nextInt();
- System.out.println("Enter the number of rows for the 1st matrix (Matrix B): ");
- int o = sc.nextInt();
- System.out.println("Enter the number of columns for the 1st matrix (Matrix B): ");
- int p = sc.nextInt();
- int[][] a = new int[s][t];
- int[][] b = new int[o][p];
- for (x=0; x< a.length; x++){
- for (y=0; y<a[0].length; y++){
- System.out.println("Enter a["+x+"]["+y+"]: ");
- a[x][y] = sc.nextInt();
- }
- }
- for (c=0; c< b.length; c++){
- for (d=0; d<b[0].length; d++){
- System.out.println("Enter b["+c+"]["+d+"]: ");
- b[x][y] = sc.nextInt();
- }
- }
- public static int[][] matrixAdd(int[][] a, int[][] b) {
- if (a.length == 0 || b.length == 0) {
- return new int[0][0];
- }
- else {
- int[][] sum = new int[a.length][a[0].length];
- for (int i = 0; i < result.length; i++) {
- for (int j = 0; j < result[0].length; j++) {
- result[i][j] = a[i][j] + b[i][j];
- }
- }
- return result;
- }
- }
- public static int[][] matrixSub(int[][] a, int[][] b) {
- if (a.length == 0 || b.length == 0) {
- return new int[0][0];
- }
- else {
- int[][] sub = new int[a.length][a[0].length];
- for (int i = 0; i < result.length; i++) {
- for (int j = 0; j < result[0].length; j++) {
- result[i][j] = a[i][j] - b[i][j];
- }
- }
- return result;
- }
- }
- public static int[][] matrixSub(int[][] a, int[][] b) {
- if (a.length == 0 || b.length == 0) {
- return new int[0][0];
- }
- else {
- int[][] sub = new int[a.length][a[0].length];
- for (int i = 0; i < result.length; i++) {
- for (int j = 0; j < result[0].length; j++) {
- result[i][j] = a[i][j] - b[i][j];
- }
- }
- return result;
- }
- }
- }
- Scanner sc = new Scanner(System.in);
- System.out.println()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement