Write a java program to add two matrices.
For example : Given two matrices A and B the sum of these two matrices is A + B.
{1, 5, 1} {1, 1, 9} {2, 6, 10}
{2, 8, 2} + {2, 2, 3} = {4, 10, 5}
{3, 7, 1} {5, 6, 4} {8, 13, 5}
Find common elements in three sorted arrays
Java Program to Add Two Matrices
In this program, I have created one method add in which i pass two matrices and the value of row and column as an argument. After adding two matrices, we print the final result.
I have added video tutorial link at the end of this post for better understanding.
Find Intersection of Two Arrays
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
/* * Add Two Matrices in Java */ public class AddTwoMatrices { public static void add(int arr1[][], int arr2[][], int n, int m) { //Declared two dimensional array int[][] arr3 = new int[n][m]; //Logic to add two matrices for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { //Sum arr3[i][j] = arr1[i][j] + arr2[i][j]; } } // Traverse and print the result for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { System.out.print(arr3[i][j] + " "); } System.out.println("\n"); } } public static void main(String[] args) { //3*3 matrices int arr1[][] = { {1, 5, 1}, {2, 8, 2}, {3, 7, 1}}; int arr2[][] = { {1, 1, 9}, {2, 2, 3}, {5, 6, 4}}; //Function call add(arr1, arr2, 3, 3); } } |
Find First Non-repeated Character in a String
In this video tutorial, I have explained how we can add two matrices. Also, I have explained how we can traverse two dimensional array. Also, I have explained java program to add two matrices.