Next-Gen App & Browser
Testing Cloud

Trusted by 2 Mn+ QAs & Devs to accelerate their release cycles

Next-Gen App & Browser Testing Cloud

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("
");
      }
   }
}

Test your website on 3000+ browsers

Get 100 minutes of automation test minutes FREE!!

Test Now...

KaneAI - Testing Assistant

World’s first AI-Native E2E testing agent.

...
ShadowLT Logo

Start your journey with LambdaTest

Get 100 minutes of automation test minutes FREE!!

Signup for free