Dain1234 / Java-Programming

0 stars 0 forks source link

Matrix multiplication #16

Open Dain1234 opened 1 year ago

Dain1234 commented 1 year ago

package com.dain.logics;

import java.util.Scanner;

public class Mat_mul { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter the the no of rows:"); int rows = sc.nextInt(); System.out.println("Enter the number of columns:"); int cols = sc.nextInt(); System.out.println("Enter the matrix 1:"); int[][] mat1 = new int[rows][cols]; int[][] mat2 = new int[rows][cols]; for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) {

            mat1[i][j] = sc.nextInt();
        }
        System.out.println();
    }
    System.out.println("Enter the second matrix:");
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            mat2[i][j] = sc.nextInt();
        }
        System.out.println();
    }
    int[][] res = new int[rows][cols];

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

            res[i][j] = mat1[i][j] + mat2[i][j];
        }

    }
    System.out.println("The added matrix is:");
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            System.out.print(res[i][j] + "\t");
        }
        System.out.println();
    }
}

}