← All resources

Write a program to input a matrix of size m by n and print the product obtained when multiplied by its transpose

Write a program to input a matrix of size m by n and print the product obtained when multiplied by its transpose

<h2>Output</h2>
Enter the number of rows:
2
Enter the number of columns:
3
Enter the values for the array:
12
21
11
13
54
23
Original Array:
12	21	11
13	54	23
Transpose Array:
12	13
21	54
11	23
Multiplied Array:
706	1543
1543	3614
import java.io.*;
public class Transpose_Multiply
{
    public static void main()throws IOException
    {
        BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
        int m, n;
        System.out.println("Enter the number of rows:");
        m = Integer.parseInt(r.readLine());
        System.out.println("Enter the number of columns:");
        n = Integer.parseInt(r.readLine());
        int a[][] = new int[m][n], b[][] = new int[n][m], i, j, s=0, k;
        System.out.println("Enter the values for the array:");
        for(i=0;i < m;i++)
        {
            for(j=0;j < n;j++)
            {       //Taking the values of the array from the user
                a[i][j] = Integer.parseInt(r.readLine());
            }
        }
        for(i=0;i < m;i++)
        {
            for(j=0;j < n;j++)
            {       //Storing the transpose of the array in array B
                b[j][i] = a[i][j];
            }
        }
        int c[][] = new int[m][m];
        for(i=0;i < m;i++)
        {
            for(j=0;j < m;j++)
            {       //Multiplying the two arrays using matrix
                    //Multiplication rule
                s=0;
                for(k=0;k < n;k++)
                {
                    s+=a[i][k]*b[k][j];
                }
                c[i][j] = s;
            }
        }
        //Printing the various arrays
        System.out.println("Original Array:");
        for(i=0;i < m;i++)
        {
            for(j=0;j < n;j++)
            {
                System.out.print(a[i][j]+"t");
            }
            System.out.println();
        }
        //Printing the various arrays
        System.out.println("Transpose Array:");
        for(i=0;i < n;i++)
        {
            for(j=0;j < m;j++)
            {
                System.out.print(b[i][j]+"t");
            }
            System.out.println();
        }
        //Printing the various arrays
        System.out.println("Multiplied Array:");
        for(i=0;i < m;i++)
        {
            for(j=0;j < m;j++)
            {
                System.out.print(c[i][j]+"t");
            }
            System.out.println();
        }
    }
}