Continuous Test Orchestration And Execution Platform Online

Perform automated and live-interactive testing on 3000+ real desktop and mobile devices online.

Write a Java program to add two matrices?

In this software testing question, we will write a program in Java that finds and prints the addition result of any two given matrices.


import java.util.Scanner;


public class AddMatrix
{
   public static void main(String[] args)
   {
      int i, j;
      int[][] m = new int[3][3];
      int[][] n = new int[3][3];
      int[][] o = new int[3][3];
     
      Scanner sc = new Scanner(System.in);
     
      System.out.print("Enter the nine elements for the first matrix: ");
      for(i=0; i<3; i++)
      {
         for(j=0; j<3; j++)
         {
            m[i][j] = sc.nextInt();
         }
      }
     
      System.out.print("Enter the nine elements for the second matrix: ");
      for(i=0; i<3; i++)
      {
         for(j=0; j<3; j++)
         {
            n[i][j] = sc.nextInt();
         }
      }
     
      for(i=0; i<3; i++)
      {
         for(j=0; j<3; j++)
         {
            o[i][j] = m[i][j] + n[i][j];
         }
      }
     
      System.out.println("Addition of Two Matrix");
      for(i=0; i<3; i++)
      {
         for(j=0; j<3; j++)
         {
            System.out.print(o[i][j]+ " ");
         }
         System.out.print("
");
      }
   }
}

LambdaTest

Test your websites, web-apps, or mobile apps seamlessly with LambdaTest.

Start Free Testing
LambdaTest

Earn resume-worthy Selenium certifications that help you land a top job.

Learn More
LambdaTest

Test your web or mobile apps

Test orchestration and execution cloud of 3000+ browsers and OS

Support

24/7 support

Security

Enterprise grade security

Cloud

Fastest test execution cloud