Java

Write a program to read two matrix of size N*N to perform multiplication and store the result in third array.

import java.util.*;

class MatrixMultiplication
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);

int i, j, k;
int array_1[][];
int array_2[][];
int array_3[][];

System.out.println("\nNumber of row and column must be same");

System.out.print("Enter number of row : ");
int row = sc.nextInt();

System.out.print("Enter number of column : ");
int column = sc.nextInt();

array_1 = new int[row][column];
array_2 = new int[row][column];
array_3 = new int[row][column];

System.out.println("\n--- Enter element in 1st array ---\n");
for(i = 0; i < row; i++)
{
for(j = 0; j < column; j++)
{
System.out.print("Enter Element " + "[" + i + "] " +  "[" + j + "] " + " : ");
array_1[i][j] = sc.nextInt();
}
System.out.println();
}

System.out.println("\n--- Enter element in 2nd array ---\n");
for(i = 0; i < row; i++)
{
for(j = 0; j < column; j++)
{
System.out.print("Enter Element " + "[" + i + "] " +  "[" + j + "] " + " : ");
array_2[i][j] = sc.nextInt();
}
System.out.println();
}

for(i = 0; i < row; i++)
{
for(j = 0; j < column; j++)
{
for(k = 0; k < row; k++)
{
array_3[i][j] += array_1[i][k] * array_2[k][j];
}
}
}

System.out.println("Multiplication of matrix");
for(i = 0; i < row; i++)
{
for(j = 0; j < column; j++)
{
System.out.print(array_3[i][j] + "\t");
}
System.out.print("\n");
}
}
}


Output :


Post a Comment

1 Comments

Emoji
(y)
:)
:(
hihi
:-)
:D
=D
:-d
;(
;-(
@-)
:P
:o
:>)
(o)
:p
(p)
:-s
(m)
8-)
:-t
:-b
b-(
:-#
=p~
x-)
(k)