Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class TheMatrix {
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- System.out.println("Enter number of rows :");
- int rows = scan.nextInt();
- System.out.println("Enter number of columns :");
- int columns = scan.nextInt();
- int x = 3;
- int y = 3;
- int[][] firstMatrix = { {2, 3, 4}, {5, 2, 3} };
- int[][] secondMatrix = { {-4, 5, 3}, {5, 6, 3} };
- int[][] addingFirstMatrix = new int[rows][columns];
- // Adding Two matrices
- for(int i = 0; i < rows; i++) {
- for (int j = 0; j < columns; j++) {
- System.out.println(" Enter the number of position " + i + "," + j + " in matrix");
- addingFirstMatrix[i][j] = scan.nextInt();
- }
- }
- int[][] addingSecondMatrix = new int[rows][columns];
- for(int i = 0; i < rows; i++) {
- for (int j = 0; j < columns; j++) {
- System.out.println(" Enter the number of position " + i + "," + j + " in matrix");
- addingSecondMatrix[i][j] = scan.nextInt();
- }
- }
- // sum of two matrices
- int[][] sum = new int[rows][columns];
- for(int i = 0; i < rows; i++) {
- for (int j = 0; j < columns; j++) {
- sum[i][j] = addingFirstMatrix[i][j] + addingSecondMatrix[i][j];
- }
- }
- // sum
- System.out.println("Sum of two matrices is: ");
- for(int[] row : sum) {
- for (int column : row) {
- System.out.print(column + " ");
- }
- System.out.println();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement