DEV Community

Saravanan
Saravanan

Posted on

1 1 1 1 1

Matrix Addition,Matrix Multipliction

1.Matrix Addition:

public static void main(String[] args) {

int[][] a= {{1,2,3}
            {1,2,3},
        {4,5,6}};

int[][] b= {{6,7,8},
        {1,2,3},
        {7,8,9}};
int [][] c=new int[a.length][a[0].length]; 

for(int row=0;row<a.length;row++)
{
    for(int col=0;col<b.length;col++)
    {   
      c[row][col]=a[row][col]+b[row][col];
      System.out.print(c[row][col]+" ");
    }
      System.out.println();
}

}
Enter fullscreen mode Exit fullscreen mode

output:
7 9 11
2 4 6
11 13 15

2.Martix Multipliction:

public static void main(String[] args) {

int[][] a= {{1,2,3},
        {1,2,3},
        {4,5,6}};
int[][] b= {{6,7,8},
        {1,2,3},
        {7,8,9}};
int [][] c=new int[a.length][a[0].length];
for(int row=0;row<a.length;row++)
{
  for(int col=0;col<b.length;col++)
  {
    for(int k = 0;k<b.length;k++)
    {
      c[row][col]=c[row][col]+(a[row][k]*b[k][col]);
    }
    System.out.print(c[row][col]+" ");
  }
     System.out.println();
}

}
Enter fullscreen mode Exit fullscreen mode

output:
29 35 41
29 35 41
71 86 101

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

DEV is better (more customized, reading settings like dark mode etc) when you're signed in!

Okay