Given two matrices, the task is to multiply them. The size and number of elements of matrices are to be read from the keyboard.
Examples:
Input: row1 = 2, col1 = 2, mat1[][] = {{1, 2}, {3, 4}}, row2 = 2, col2 = 2, mat2[][] = {{1, 1}, {1, 1}} Output: {{3, 3}, {7, 7}} Input: row1 = 2, col1 = 2, mat1[][] = {{2, 4}, {3, 4}} row1 = 2, col1 = 2, mat2[][] = {{1, 2}, {1, 3}} Output: {{6, 16}, {7, 18}}
Implementation:
C++
// C++ program to multiply two matrices. #include <bits/stdc++.h> using namespace std;
const int MAX = 100;
// Function to print Matrix void printMatrix( int M[][MAX], int rowSize, int colSize)
{ for ( int i = 0; i < rowSize; i++) {
for ( int j = 0; j < colSize; j++)
cout <<M[i][j] << " " ;
cout << "\n" ;
}
} // Function to multiply two matrices A[][] and B[][] void multiplyMatrix( int row1, int col1, int A[][MAX],
int row2, int col2, int B[][MAX])
{ int i, j, k;
// Matrix to store the result
int C[MAX][MAX];
// Check if multiplication is Possible
if (row2 != col1) {
cout << "Not Possible\n" ;
return ;
}
// Multiply the two
for (i = 0; i < row1; i++) {
for (j = 0; j < col2; j++) {
C[i][j] = 0;
for (k = 0; k < row2; k++)
C[i][j] += A[i][k] * B[k][j];
}
}
// Print the result
cout << "\nResultant Matrix: \n" ;
printMatrix(C, row1, col2);
} // Driven Program int main()
{ int row1, col1, row2, col2, i, j;
int A[MAX][MAX], B[MAX][MAX];
// Read size of Matrix A from user
cout << "Enter the number of rows of First Matrix: " ;
cin >> row1;
cout << row1;
cout << "\nEnter the number of columns of First Matrix: " ;
cin >> col1;
cout <<col1;
// Read the elements of Matrix A from user
cout << "\nEnter the elements of First Matrix: " ;
for (i = 0; i < row1; i++) {
for (j = 0; j < col1; j++) {
cout << "\n " <<i << j;
cin >> A[i][j];
cout << A[i][j];
}
}
// Read size of Matrix B from user
cout << "\nEnter the number of rows of Second Matrix: " ;
cin >> row2;
cout << row2;
cout << "\nEnter the number of columns of Second Matrix: " ;
cin >> col2;
cout << col2;
// Read the elements of Matrix B from user
cout << "\nEnter the elements of First Matrix: " ;
for (i = 0; i < row2; i++) {
for (j = 0; j < col2; j++) {
cout << "\n " <<i << j;
cin >> B[i][j];
cout << B[i][j];
}
}
// Print the Matrix A
cout << "\n\nFirst Matrix: \n" ;
printMatrix(A, row1, col1);
// Print the Matrix B
cout << "\nSecond Matrix: \n" ;
printMatrix(B, row2, col2);
// Find the product of the 2 matrices
multiplyMatrix(row1, col1, A, row2, col2, B);
return 0;
} // This code is contributed by abhijeet19403. |
C
// C program to multiply two matrices. #include <stdio.h> const int MAX = 100;
// Function to print Matrix void printMatrix( int M[][MAX], int rowSize, int colSize)
{ for ( int i = 0; i < rowSize; i++) {
for ( int j = 0; j < colSize; j++)
printf ( "%d " , M[i][j]);
printf ( "\n" );
}
} // Function to multiply two matrices A[][] and B[][] void multiplyMatrix( int row1, int col1, int A[][MAX],
int row2, int col2, int B[][MAX])
{ int i, j, k;
// Matrix to store the result
int C[MAX][MAX];
// Check if multiplication is Possible
if (row2 != col1) {
printf ( "Not Possible\n" );
return ;
}
// Multiply the two
for (i = 0; i < row1; i++) {
for (j = 0; j < col2; j++) {
C[i][j] = 0;
for (k = 0; k < row2; k++)
C[i][j] += A[i][k] * B[k][j];
}
}
// Print the result
printf ( "\nResultant Matrix: \n" );
printMatrix(C, row1, col2);
} // Driven Program int main()
{ int row1, col1, row2, col2, i, j;
int A[MAX][MAX], B[MAX][MAX];
// Read size of Matrix A from user
printf ( "Enter the number of rows of First Matrix: " );
scanf ( "%d" , &row1);
printf ( "%d" , row1);
printf ( "\nEnter the number of columns of First Matrix: " );
scanf ( "%d" , &col1);
printf ( "%d" , col1);
// Read the elements of Matrix A from user
printf ( "\nEnter the elements of First Matrix: " );
for (i = 0; i < row1; i++) {
for (j = 0; j < col1; j++) {
printf ( "\nA[%d][%d]: " , i, j);
scanf ( "%d" , &A[i][j]);
printf ( "%d" , A[i][j]);
}
}
// Read size of Matrix B from user
printf ( "\nEnter the number of rows of Second Matrix: " );
scanf ( "%d" , &row2);
printf ( "%d" , row2);
printf ( "\nEnter the number of columns of Second Matrix: " );
scanf ( "%d" , &col2);
printf ( "%d" , col2);
// Read the elements of Matrix B from user
printf ( "\nEnter the elements of First Matrix: " );
for (i = 0; i < row2; i++) {
for (j = 0; j < col2; j++) {
printf ( "\nB[%d][%d]: " , i, j);
scanf ( "%d" , &B[i][j]);
printf ( "%d" , B[i][j]);
}
}
// Print the Matrix A
printf ( "\n\nFirst Matrix: \n" );
printMatrix(A, row1, col1);
// Print the Matrix B
printf ( "\nSecond Matrix: \n" );
printMatrix(B, row2, col2);
// Find the product of the 2 matrices
multiplyMatrix(row1, col1, A, row2, col2, B);
return 0;
} |
Java
// Java program to multiply two matrices. import java.io.*;
import java.util.*;
class GFG{
static int MAX = 100 ;
// Function to print Matrix static void printMatrix( int M[][], int rowSize,
int colSize)
{ for ( int i = 0 ; i < rowSize; i++)
{
for ( int j = 0 ; j < colSize; j++)
System.out.print(M[i][j] + " " );
System.out.println();
}
} // Function to multiply two matrices A[][] and B[][] static void multiplyMatrix( int row1, int col1,
int A[][], int row2,
int col2, int B[][])
{ int i, j, k;
// Matrix to store the result
int C[][] = new int [MAX][MAX];
// Check if multiplication is Possible
if (row2 != col1)
{
System.out.println( "Not Possible" );
return ;
}
// Multiply the two
for (i = 0 ; i < row1; i++)
{
for (j = 0 ; j < col2; j++)
{
C[i][j] = 0 ;
for (k = 0 ; k < row2; k++)
C[i][j] += A[i][k] * B[k][j];
}
}
// Print the result
System.out.println();
System.out.println( "Resultant Matrix: " );
printMatrix(C, row1, col2);
} // Driver code public static void main(String[] args)
{ Scanner read = new Scanner(System.in);
int row1, col1, row2, col2, i, j;
int A[][] = new int [MAX][MAX];
int B[][] = new int [MAX][MAX];
// Read size of Matrix A from user
System.out.print( "Enter the number of " +
"rows of First Matrix: " );
row1 = read.nextInt();
System.out.println(row1);
System.out.print( "Enter the number of " +
"columns of First Matrix: " );
col1 = read.nextInt();
System.out.println(col1);
// Read the elements of Matrix A from user
System.out.println( "Enter the elements " +
"of First Matrix: " );
for (i = 0 ; i < row1; i++)
{
for (j = 0 ; j < col1; j++)
{
System.out.print( "A[" + i + "][" +
j + "]: " );
A[i][j] = read.nextInt();
System.out.println(A[i][j]);
}
}
// Read size of Matrix B from user
System.out.print( "Enter the number of " +
"rows of Second Matrix: " );
row2 = read.nextInt();
System.out.println(row2);
System.out.print( "Enter the number of " +
"columns of Second Matrix: " );
col2 = read.nextInt();
System.out.println(col2);
// Read the elements of Matrix B from user
System.out.println( "Enter the elements " +
"of First Matrix: " );
for (i = 0 ; i < row2; i++)
{
for (j = 0 ; j < col2; j++)
{
System.out.print( "A[" + i + "][" +
j + "]: " );
B[i][j] = read.nextInt();
System.out.println(B[i][j]);
}
}
// Print the Matrix A
System.out.println();
System.out.println( "First Matrix: " );
printMatrix(A, row1, col1);
// Print the Matrix B
System.out.println();
System.out.println( "Second Matrix: " );
printMatrix(B, row2, col2);
// Find the product of the 2 matrices
multiplyMatrix(row1, col1, A, row2, col2, B);
} } // This code is contributed by Dharanendra L V. |
Python3
# Python3 program to multiply two matrices. MAX = 100
# Function to print Matrix def printMatrix(M, rowSize, colSize) :
for i in range (rowSize) :
for j in range (colSize) :
print (M[i][j], end = " " )
print ()
# Function to multiply two matrices # A[][] and B[][] def multiplyMatrix(row1, col1, A,
row2, col2, B) :
# Matrix to store the result
C = [[ 0 for i in range ( MAX )]
for j in range ( MAX )]
# Check if multiplication is Possible
if (row2 ! = col1) :
print ( "Not Possible" )
return
# Multiply the two
for i in range (row1) :
for j in range (col2) :
C[i][j] = 0
for k in range (row2) :
C[i][j] + = A[i][k] * B[k][j];
# Print the result
print ( "Resultant Matrix: " )
printMatrix(C, row1, col2)
# Driver Code if __name__ = = "__main__" :
A = [[ 0 for i in range ( MAX )]
for j in range ( MAX )]
B = [[ 0 for i in range ( MAX )]
for j in range ( MAX )]
# Read size of Matrix A from user
row1 = int ( input ( "Enter the number of rows of First Matrix: " ))
col1 = int ( input ( "Enter the number of columns of First Matrix: " ))
# Read the elements of Matrix A from user
print ( "Enter the elements of First Matrix: " );
for i in range (row1) :
for j in range (col1) :
A[i][j] = int ( input ( "A[" + str (i) +
"][" + str (j) + "]: " ))
# Read size of Matrix B from user
row2 = int ( input ( "Enter the number of rows of Second Matrix: " ))
col2 = int ( input ( "Enter the number of columns of Second Matrix: " ))
# Read the elements of Matrix B from user
print ( "Enter the elements of Second Matrix: " );
for i in range (row2) :
for j in range (col2) :
B[i][j] = int ( input ( "B[" + str (i) +
"][" + str (j) + "]: " ))
# Print the Matrix A
print ( "First Matrix: " )
printMatrix(A, row1, col1)
# Print the Matrix B
print ( "Second Matrix: " )
printMatrix(B, row2, col2)
# Find the product of the 2 matrices
multiplyMatrix(row1, col1, A, row2, col2, B)
# This code is contributed by Ryuga |
C#
// C# program to multiply two matrices. using System;
class GFG
{ const int MAX = 100;
static void printMatrix( int [,] M, int rowSize, int colSize)
{
for ( int i = 0; i < rowSize; i++)
{
for ( int j = 0; j < colSize; j++)
Console.Write(M[i, j] + " " );
Console.WriteLine();
}
}
static void multiplyMatrix( int row1, int col1, int [,] A,
int row2, int col2, int [,] B)
{
int i, j, k;
// Matrix to store the result
int [,] C = new int [MAX, MAX];
// Check if multiplication is Possible
if (row2 != col1)
{
Console.WriteLine( "Not Possible" );
return ;
}
// Multiply the two
for (i = 0; i < row1; i++)
{
for (j = 0; j < col2; j++)
{
C[i, j] = 0;
for (k = 0; k < row2; k++)
C[i, j] += A[i, k] * B[k, j];
}
}
// Print the result
Console.WriteLine( "\nResultant Matrix: " );
printMatrix(C, row1, col2);
}
static void Main( string [] args)
{
int row1, col1, row2, col2, i, j;
int [,] A = new int [MAX, MAX];
int [,] B = new int [MAX, MAX];
// Read size of Matrix A from user
Console.Write( "Enter the number of rows of First Matrix: " );
row1 = Convert.ToInt32(Console.ReadLine());
Console.Write( "Enter the number of columns of First Matrix: " );
col1 = Convert.ToInt32(Console.ReadLine());
// Read the elements of Matrix A from user
Console.WriteLine( "Enter the elements of First Matrix: " );
for (i = 0; i < row1; i++)
{
for (j = 0; j < col1; j++)
{
Console.Write( "[" + i + "][" + j + "] = " );
A[i, j] = Convert.ToInt32(Console.ReadLine());
}
}
// Read size of Matrix B from user
Console.Write( "Enter the number of rows of Second Matrix: " );
row2 = Convert.ToInt32(Console.ReadLine());
Console.Write( "Enter the number of columns of Second Matrix: " );
col2 = Convert.ToInt32(Console.ReadLine());
// Read the elements of Matrix B from user
Console.WriteLine( "Enter the elements of Second Matrix: " );
for (i = 0; i < row2; i++)
{
for (j = 0; j < col2; j++)
{
Console.Write( "[" + i + "][" + j + "] = " );
B[i, j] = Convert.ToInt32(Console.ReadLine());
}
}
// Print the Matrix A
Console.WriteLine( "\nFirst Matrix: " );
printMatrix(A, row1, col1);
// Print the Matrix B
Console.WriteLine( "\nSecond Matrix: " );
printMatrix(B, row2, col2);
// Find the product of the 2 matrices
multiplyMatrix(row1, col1, A, row2, col2, B);
Console.ReadLine();
}
} |
Javascript
// javascript program to multiply two matrices. let MAX = 100; // Function to print Matrix function printMatrix(M, rowSize, colSize)
{ for (let i = 0; i < rowSize; i++) {
for (let j = 0; j < colSize; j++)
process.stdout.write(M[i][j] + " " );
process.stdout.write( "\n" );
}
} // Function to multiply two matrices A[][] and B[][] function multiplyMatrix(row1, col1, A,
row2, col2, B)
{ let i, j, k;
// Matrix to store the result
let C = new Array(MAX);
for (let i = 0; i < C.length; i++){
C[i] = new Array(MAX);
}
// Check if multiplication is Possible
if (row2 != col1) {
process.stdout.write( "Not Possible\n" );
return ;
}
// Multiply the two
for (let i = 0; i < row1; i++) {
for (let j = 0; j < col2; j++) {
C[i][j] = 0;
for (k = 0; k < row2; k++)
C[i][j] += A[i][k] * B[k][j];
}
}
// Print the result
process.stdout.write( "\nResultant Matrix: \n" );
printMatrix(C, row1, col2);
} // Driven Program let row1, col1, row2, col2, i, j; let A = new Array(MAX);
for (let i = 0; i < A.length; i++) A[i] = new Array(MAX);
let B = new Array(MAX);
for (let i = 0; i < B.length; i++) B[i] = new Array(MAX);
// Read size of Matrix A from user process.stdout.write( "Enter the number of rows of First Matrix: " );
row1 = prompt(); console.log(row1); process.stdout.write( "Enter the number of columns of First Matrix: " );
col1 = prompt(); console.log(col1); // Read the elements of Matrix A from user console.log( "Enter the elements of First Matrix: " );
for (let i = 0; i < row1; i++) {
for (let j = 0; j < col1; j++) {
console.log();
console.log(i + " " + j);
A[i][j] = prompt();
process.stdout.write(A[i][j] + " " );
}
} // Read size of Matrix B from user process.stdout.write( "\nEnter the number of rows of Second Matrix: " );
row2 = prompt(); process.stdout.write(row2); process.stdout.write( "\nEnter the number of columns of Second Matrix: " );
col2 = prompt(); process.stdout.write(col2); // Read the elements of Matrix B from user process.stdout.write( "\nEnter the elements of First Matrix: " );
for (i = 0; i < row2; i++) {
for (j = 0; j < col2; j++) {
process.stdout.write( "\n " + i + " " + j);
B[i][j] = prompt();
process.stdout.write(B[i][j] + " " );
}
} // Print the Matrix A process.stdout.write( "\n\nFirst Matrix: \n" );
printMatrix(A, row1, col1); // Print the Matrix B process.stdout.write( "\nSecond Matrix: \n" );
printMatrix(B, row2, col2); // Find the product of the 2 matrices multiplyMatrix(row1, col1, A, row2, col2, B); // This code is contributed by Nidhi goel. |
Output:
Enter the number of rows of First Matrix: 2 Enter the number of columns of First Matrix: 3 Enter the elements of First Matrix: A[0][0]: 1 A[0][1]: 2 A[0][2]: 3 A[1][0]: 4 A[1][1]: 5 A[1][2]: 6 Enter the number of rows of Second Matrix: 3 Enter the number of columns of Second Matrix: 2 Enter the elements of First Matrix: B[0][0]: 1 B[0][1]: 2 B[1][0]: 3 B[1][1]: 4 B[2][0]: 5 B[2][1]: 6 First Matrix: 1 2 3 4 5 6 Second Matrix: 1 2 3 4 5 6 Resultant Matrix: 22 28 49 64
Complexity Analysis:
- Time Complexity: O(row1 * col2 * row2)
- Auxiliary Space: O(row1 * col2)
Method 2:
We can use the NumPy library in Python to perform matrix multiplication operations and take input from the user.
NOTE: We do not need to explicitly check for the possibility of multiplication of matrices using NumPy, it automatically throws an error if the multiplication is not possible.
Python3
import numpy as np
# Function to multiply two matrices A and B def multiplyMatrix(A, B):
C = np.dot(A, B)
return C
# Driver Code if __name__ = = "__main__" :
# Read size of Matrix A from user
row1 = int ( input ( "Enter the number of rows of First Matrix: " ))
col1 = int ( input ( "Enter the number of columns of First Matrix: " ))
# Read the elements of Matrix A from user
print ( "Enter the elements of First Matrix: " );
A = np.zeros((row1, col1))
for i in range (row1) :
for j in range (col1) :
A[i][j] = int ( input ( "A[" + str (i) +
"][" + str (j) + "]: " ))
# Read size of Matrix B from user
row2 = int ( input ( "Enter the number of rows of Second Matrix: " ))
col2 = int ( input ( "Enter the number of columns of Second Matrix: " ))
# Read the elements of Matrix B from user
print ( "Enter the elements of Second Matrix: " );
B = np.zeros((row2, col2))
for i in range (row2) :
for j in range (col2) :
B[i][j] = int ( input ( "B[" + str (i) +
"][" + str (j) + "]: " ))
# Print the Matrix A
print ( "First Matrix: " )
print (A)
# Print the Matrix B
print ( "Second Matrix: " )
print (B)
# Find the product of the 2 matrices
C = multiplyMatrix(A, B)
# Print the result
print ( "Resultant Matrix: " )
print (C)
|
Output:
Time Complexity: O(n^3)
Space Complexity: O(n^2)
Recommended Articles