Matrix

mail@pastecode.io avatar
unknown
java
2 years ago
846 B
14
Indexable
Never
// Matrix multiplication

public class Main
{
  public static void main (String[]args)
  {
    //creating two matrices    
    int a[][]={{1,1,1},
               {2,2,2},
               {3,3,3}};
               
    int b[][]={{1,1,1},
               {2,2,2},
               {3,3,3}};
               
     //creating another matrix to store the multiplication of two matrices    
    int c[][]=new int[3][3]; 
    
    for(int i=0;i<3;i++)
    {    
        for(int j=0;j<3;j++)
        {  
            c[i][j]=0;     
            for(int k=0;k<3;k++)      
            {      
               c[i][j]+=a[i][k]*b[k][j];      
            }   
        } 
    }
    for(int i=0;i<3;i++)
    {    
        for(int j=0;j<3;j++)
        {  
            System.out.print(c[i][j] + " ");
        } 
        System.out.print("\n"); // newline
    }
  }
}