Q:

Write a Java Program for Multiplication of Two Matrices

belongs to collection: Java Arrays Solved Programs

0

To multiply two matrices in Java Programming, you have to first ask to the user to enter the number of rows and columns of the first matrix and then ask to enter the first matrix elements.

Again ask the same for the second matrix.

Now start multiplying the two matrices and store the multiplication result inside any variable say mul and finally store the value of mul in the third matrix say multiply[ ][ ] at the equivalent index as shown in the following program.

  • Following Java Program ask to the user to enter the two n*n matrices elements, to multiply them to form a new matrix which is the multiplication result of the two entered n*n matrices, then display the result on the screen:

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

SOURCE CODE ::

import java.util.Scanner;

public class Mularrays {

    public static void main(String[] args) {
        
        int i,j,m,n,p,q,sum=0;
        
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter number of rows of first matrix:");
        m=sc.nextInt();
        System.out.print("Enter number of columns of first matrix:");
        n=sc.nextInt();
        System.out.println("");
        System.out.print("Enter number of rows of second matrix:");
        p=sc.nextInt();
        System.out.print("Enter number of columns of second matrix:");
        q=sc.nextInt();
        System.out.println("");
        
        if(n!=p)
        System.out.println("Sorry multiplication can’t be done!!");
        else 
        {
        int a[][]=new int[m][n];
        int b[][]=new int[p][q];
        int mul[][]=new int[m][q];
        
//--------------Enter_data---------------------------------------------
        
System.out.println("Enter values of first matrix :: ");
        for(i=0;i<m;i++)
        {
            for(j=0;j<n;j++)
            {
                System.out.print(" "+i+j+" position:");
                a[i][j]=sc.nextInt();
            }
        }
        System.out.println("Enter values of 2nd matrix :: ");
        for(i=0;i<p;i++)
        {
            for(j=0;j<q;j++)
            {
                System.out.print(" "+i+j+" position:");
                b[i][j]=sc.nextInt();
            }
        }
//----------------Display-------------------------------------------------
        
        System.out.println("Ist matrix :: ");
        for(i=0;i<m;i++)
        {
            for(j=0;j<n;j++)
            {
                System.out.print(" "+a[i][j]);
              
            }
            System.out.println("");
        }
        
        System.out.println("2nd matrix :: ");
        for(i=0;i<p;i++)
        {
            for(j=0;j<q;j++)
            {
                System.out.print(" "+b[i][j]);
              
            }
            System.out.println("");
        }
//----------------Multiplication-------------------------------------------
        
        for(i=0;i<m;i++)
        {
            for(j=0;j<q;j++)
            {
                mul[i][j]=0;
                
                for(int k=0;k<n;k++)
                {
                    mul[i][j] = mul[i][j] + a[i][k]*b[k][j];
                }
            }
        }
        
//-------------Display_multiplication_matrix--------------------------------

        System.out.println("Multiplication of matrices are:: ");
        for(i=0;i<m;i++)
        {
            for(j=0;j<q;j++)
            {
                System.out.print(" "+mul[i][j]);
              
            }
            System.out.println("");
        }
        
    }
    
}
}

OUTPUT :

Enter number of rows of first matrix:3
Enter number of columns of first matrix:2

Enter number of rows of second matrix:2
Enter number of columns of second matrix:3

Enter values of first matrix :: 
 00 position:1
 01 position:2
 10 position:3
 11 position:4
 20 position:5
 21 position:6
Enter values of 2nd matrix :: 
 00 position:1
 01 position:2
 02 position:3
 10 position:4
 11 position:5
 12 position:6
Ist matrix :: 
 1 2
 3 4
 5 6
2nd matrix :: 
 1 2 3
 4 5 6
Multiplication of matrices are:: 
 9 12 15
 19 26 33
 29 40 51

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Write a Java Program for Linear Search on unsorted... >>
<< Write a Java Program to find Transpose Matrix...