BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java Program to Add Two Matrix using Multi-dimensional Arrays

By Chaitanya Singh | Filed Under: Java Examples

Here we will write a java program to add two given matrices and display the output matrix that will be the sum of given matrices.

Example: Java Program to add two given Matrices

In the following example we have two matrices MatrixA and MatrixB, we have declared these matrices as multi-dimensional arrays.

Two matrices can only be added or subtracted only if they have same dimension which means they must have the same number of rows and columns. Here we have two MatrixA and MatrixB which have same rows and columns. The addition of these matrices will have same rows and columns.

This is how we declare a matrix as multi-dimensional array:
Matrix: This matrix has two rows and four columns.

| 1 1 1 1 |
| 2 3 5 2 |

The declaration of this matrix as 2D array:

int[][] MatrixA = { {1, 1, 1, 1}, {2, 3, 5, 2} };

We are using for loop to add the corresponding elements of both the matrices and store the addition values in sum matrix. For example: sum[0][0] = MatrixA[0][0] + MatrixB[0][0], similarly sum[0][1] = MatrixA[0][1] + MatrixB[0][1] and so on.

public class JavaExample {
    public static void main(String[] args) {
        int rows = 2, columns = 4;
        
        // Declaring the two matrices as multi-dimensional arrays
        int[][] MatrixA = { {1, 1, 1, 1}, {2, 3, 5, 2} };
        int[][] MatrixB = { {2, 3, 4, 5}, {2, 2, 4, -4} };
        
        /* Declaring a matrix sum, that will be the sum of MatrixA
         * and MatrixB, the sum matrix will have the same rows and
         * columns as the given matrices.
         */
        int[][] sum = new int[rows][columns];
        for(int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++) {
                sum[i][j] = MatrixA[i][j] + MatrixB[i][j];
            }
        }
        // Displaying the sum matrix
        System.out.println("Sum of the given matrices is: ");
        for(int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++) {
                System.out.print(sum[i][j] + "    ");
            }
            System.out.println();
        }
    }
}

Output:
Java Program to Add Two Matrices using Multi-dimensional Arrays

Related Java Examples

  1. Java Program to add two binary numbers
  2. Java program to add two complex numbers
  3. Java program to add two numbers
  4. Java program to calculate power of a number
  5. Java program to find Quotient and Remainder

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Programs

  • C Programs
  • Java Programs
  • C++ Programs

Java Examples

  • Check Odd-even
  • Linear Search
  • Binary Search
  • Floyd's Triangle
  • Reverse number
  • Random Number
  • first n prime numbers
  • Disp prime Numbers
  • Check Prime number
  • Palindrome String
  • Find factorial
  • Sum of elements of Array
  • Area of rectangle
  • Area of Square
  • Area of Triangle
  • Circle

Tutorials

  • Java Tutorial
  • OOPs Concepts
  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap