To multiply 2D array in java

CODE :

public class P_02_2dArrayMultiplication{
public static void main(String[] args) {

        int n = 2;
        int[][] a = {{1,2},{4,5}};
        int[][] b = {{2,1},{5,6}};
        int[][] c = new int[n][n];
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                for (int k = 0; k < n; k++)
                {
                    c[i][j] = c[i][j] + a[i][k] * b[k][j];
                }
            }
        }
        System.out.println("The product is:");
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                System.out.print(c[i][j] + " ");
            }
            System.out.println();
        }
}

}

Output :



Comments

Popular posts from this blog

To create an html page with frames and frameset

1. Write a program to insert a new element at end as well as at a given position in an array in Java

1. To familiarize with network devices like switch, hub, routers and bridges in LaTex