Program to Find the multiplication of two matrices

Problem Statement:- Program to find the multiplication of two matrices or 2D Array.

Sample Input/Output:-


Sample Input First: 2 2

9 3 6 2


5 7 2 8

Sample Output First: 

51 138 172 230

Sample Input Second:2 3 

11 12 13

14 15 16


2 3 4 5 2 1

Sample Output Second: 

82 139 195 298 370 441


Data requirement:-

  Input Data:- row_size, col_size, matrix[][], matrix1[][]


  Output Data:-mul_matrix[][]


  Additional Data:- i, j,k


Program in C

Here is the source code of the C Program to find the multiplication of two matrices.


Code:


#include<stdio.h> int main() { int row_size,col_size; //Get size of matrix printf("Enter the row Size Of the Matrix:"); scanf("%d",&row_size); printf("Enter the columns Size Of the Matrix:"); scanf("%d",&col_size); int matrix[row_size][col_size]; //Taking input of the 1st matrix int i,j; printf("Enter the 1st Matrix Element:\n"); for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { scanf("%d",&matrix[i][j]); } } int matrix1[row_size][col_size]; //Taking input of the 2nd matrix printf("Enter the 2nd Matrix Element:\n"); for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { scanf("%d",&matrix1[i][j]); } } int mul_matrix[row_size][col_size],k,sum=0; //Compute Multiplication of two matrices for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { for ( k = 0 ; k < row_size; k ++ ) { sum+= matrix[i][k] * matrix1[k][j]; } mul_matrix[i][j]=sum; } } //display the Multiplication of two matrices printf("Multiplication of the two Matrices is:\n"); for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { printf("%d ",mul_matrix[i][j]); } printf("\n"); } }


Input/Output:

Enter the row Size Of the Matrix:2 Enter the columns Size Of the Matrix:2 Enter the 1st Matrix Element: 9 3 6 2 Enter the 2nd Matrix Element: 5 7 2 8 Multiplication of the two Matrices is: 51 138 172 230


Program in C++

Here is the source code of the C++ Program to perform the multiplication of two 2*3 matrices.


Code:


#include<iostream> using namespace std; int main() { int row_size,col_size; //Get size of matrix cout<<"Enter the row Size Of the Matrix:"; cin>>row_size; cout<<"Enter the columns Size Of the Matrix:"; cin>>col_size; int matrix[row_size][col_size]; //Taking input of the 1st matrix int i,j; cout<<"Enter the 1st Matrix Element:\n"; for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { cin>>matrix[i][j]; } } int matrix1[row_size][col_size]; //Taking input of the 2nd matrix cout<<"Enter the 2nd Matrix Element:\n"; for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { cin>>matrix1[i][j]; } } int mul_matrix[row_size][col_size],sum=0,k; //Compute Multiplication of two matrices for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { for ( k = 0 ; k < row_size; k ++ ) { sum+= matrix[i][k] * matrix1[k][j]; } mul_matrix[i][j]=sum; } } //display the Multiplication of two matrices cout<<"Multiplication of the two Matrices is:\n"; for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { cout<<mul_matrix[i][j]<<" "; } cout<<"\n"; } }


Input/Output:

Enter the row Size Of the Matrix:2 Enter the columns Size Of the Matrix:3 Enter the 1st Matrix Element: 11 12 13 14 15 16 Enter the 2nd Matrix Element: 2 3 4 5 2 1 Multiplication of the two Matrices is: 82 139 195 298 370 441


Program in Java

Here is the source code of the Java Program to Program to find the product of two matrices.


Code:


import java.util.Scanner; public class MultiplicationOfTwoMatrices { public static void main(String[] args) { Scanner cs=new Scanner(System.in); int row_size,col_size; //Get size of matrix System.out.print("Enter the row Size Of the Matrix:"); row_size=cs.nextInt(); System.out.print("Enter the columns Size Of the Matrix:"); col_size=cs.nextInt(); int matrix[][] =new int[row_size][col_size]; //Taking input of the 1st matrix int i,j; System.out.println("Enter the 1st Matrix Element:"); for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { matrix[i][j]=cs.nextInt(); } } int matrix1[][] =new int[row_size][col_size]; int k,sum=0; //Taking input of the 2nd matrix System.out.println("Enter the 2nd Matrix Element:"); for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { matrix1[i][j]=cs.nextInt(); } } int mul_matrix[][] =new int[row_size][col_size]; //Compute Multiplication of two matrices for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { for ( k = 0 ; k < row_size; k ++ ) { sum+= matrix[i][k] * matrix1[k][j]; } mul_matrix[i][j]=sum; } } //display the 1st Matrix System.out.println("Given 1st Matrix is:"); for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { System.out.print(matrix[i][j]+" "); } System.out.println(); } //display the 2nd Matrix System.out.println("Given 2nd Matrix is:"); for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { System.out.print(matrix1[i][j]+" "); } System.out.println(); } //Compute Multiplication of two matrices System.out.println("Multiplication of the two Matrices is:"); for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { System.out.print(mul_matrix[i][j]+" "); } System.out.println(); } cs.close(); } }


Input/Output:

Enter the row Size Of the Matrix:3 Enter the columns Size Of the Matrix:3 Enter the 1st Matrix Element: 4 5 6 7 8 9 2 3 4 Enter the 2nd Matrix Element: 1 2 1 1 1 1 1 1 1 Multiplication of the two Matrices is: 15 34 49 73 104 128 137 148 157



Program in Python

Here is the source code of the Python Program to find the multiplication of two matrices.


Code:


# Get size of matrix row_size=int(input("Enter the row Size Of the Matrix:")) col_size=int(input("Enter the columns Size Of the Matrix:")) matrix=[] # Taking input of the 1st matrix print("Enter the Matrix Element:") for i in range(row_size): matrix.append([int(j) for j in input().split()]) matrix1=[] # Taking input of the 2nd matrix print("Enter the Matrix Element:") for i in range(row_size): matrix1.append([int(j) for j in input().split()]) sum=0 # Compute Multiplication of two matrices mul_matrix=[[0 for i in range(col_size)] for i in range(row_size)] for i in range(len(matrix)): for j in range(len(matrix[0])): for k in range(row_size): sum+=matrix[i][j]*matrix1[i][j] mul_matrix[i][j]=sum # display the Multiplication of two matrices print("Multiplication of the two Matrices is:") for m in mul_matrix: print(m)


Input/Output:

Enter the row Size Of the Matrix:2 Enter the columns Size Of the Matrix:2 Enter the Matrix Element: 20 30 40 60 Enter the Matrix Element: 2 3 4 6 Multiplication of the two Matrices is: [80, 260] [580, 1300]


Most Recommend Questions:-








More Questions:-




Array Programming Questions and Solutions(50)

C/C++/Java/Python Practice Question

Post a Comment

0 Comments