VOOZH about

URL: https://www.geeksforgeeks.org/java/java-program-to-add-two-matrices/

⇱ Java Program to Add two Matrices - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Java Program to Add two Matrices

Last Updated : 12 Jul, 2025

Given two matrices A and B of the same size, the task is to add them in Java. 

Examples:

Input: A[][] = {{1, 2}, {3, 4}} B[][] = {{1, 1}, {1, 1}} Output: {{2, 3}, {4, 5}} Input: A[][] = {{2, 4}, {3, 4}} B[][] = {{1, 2}, {1, 3}} Output: {{3, 6}, {4, 7}}

Program to Add Two Matrices in Java

Follow the Steps to Add Two Matrices in Java as mentioned below:

  • Take the two matrices to be added.
  • Create a new Matrix to store the sum of the two matrices.
  • Traverse each element of the two matrices and add them. Store this sum in the new matrix at the corresponding index.
  • Print the final new matrix.

Below is the implementation of the above approach: 


Output
Resultant Matrix:
2 3 
4 5 
Comment
Article Tags: