Program to Multiply two Matrices

Description

* To multiply two matrices, the number of columns of the first matrix should be equal 
   to the number of rows of the second matrix.
* If multiplication is not possible, then it should print error message.

C/C++

/* C program to multiply two matrices */
//Save it as MultiplyTwoMatrix.c

#include<stdio.h>
int main(){

    int i, j, k, row1, column1, row2, column2, sum=0;

    printf("Enter the number of rows of first matrix : ");
    scanf("%d",&row1);

    printf("Enter the number of columns of first matrix : ");
    scanf("%d",&column1);
    
    //Declaring first matrix
    int matrix1[row1][column1];
    
    //Reading input for first matrix
    printf("Enter the elements of first matrix : \n");
    for(i=0;i<row1;i++) {
        for(j=0;j<column1;j++) {
            printf("matrix1[%d][%d] : ",i,j);
            scanf("%d",&matrix1[i][j]);
        }
    }
    
    //Displaying Output for first matrix
    printf("The elements of first matrix : \n");
    for(i=0;i<row1;i++) {
        for(j=0;j<column1;j++) {
            printf("%d ",matrix1[i][j]);
        }
        printf("\n");
    }

    printf("Enter the number of rows of second matrix : ");
    scanf("%d",&row2);

    printf("Enter the number of columns of second matrix : ");
    scanf("%d",&column2);

    //Declaring second matrix
    int matrix2[row2][column2];

    //Reading input for second matrix
    printf("Enter the elements of second matrix : \n");
    for(i=0;i<row2;i++) {
        for(j=0;j<column2;j++) {
            printf("matrix2[%d][%d] : ",i,j);
            scanf("%d",&matrix2[i][j]);
        }
    }

    //Displaying Output for second matrix
    printf("The elements of second matrix : \n");
    for(i=0;i<row2;i++) {
        for(j=0;j<column2;j++) {
            printf("%d ",matrix2[i][j]);
        }
        printf("\n");
    }

    //Declaring Result matrix after multiplication
    int matrix3[row1][column2];

    if(column1!=row2){
        printf("Matrix cannot be multiplied");
    }
    else{
        for (i = 0; i < row1; i++) {
            for (j = 0; j < column2; j++) {
                for (k = 0; k < column1; k++) {
                    sum = sum + matrix1[i][k]*matrix2[k][j];
                }
                matrix3[i][j] = sum;
                sum = 0;
            }
        }
        //Displaying Output after multiplication
        printf("The matrix after multiplication is : \n");
        for(i=0;i<row1;i++) {
            for(j=0;j<column2;j++) {
                printf("%d ",matrix3[i][j]);
            }
            printf("\n");
        }
    }
    return 0;
}
Input:
Enter the number of rows of first matrix : 2
Enter the number of columns of first matrix : 3
Enter the elements of first matrix :
matrix1[0][0] : 1
matrix1[0][1] : 2
matrix1[0][2] : 3
matrix1[1][0] : 4
matrix1[1][1] : 5
matrix1[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 second matrix :
matrix2[0][0] : 1
matrix2[0][1] : 4
matrix2[1][0] : 2
matrix2[1][1] : 5
matrix2[2][0] : 3
matrix2[2][1] : 6

Output:
The elements of first matrix :
1 2 3
4 5 6
The elements of second matrix :
1 4
2 5
3 6
The matrix after multiplication is :
14 32
32 77

Java

/* Java program to multiply two matrices */
//Save it as MultiplyTwoMatrix.java

import java.io.*;
import java.util.Scanner;
public class MultiplyTwoMatrix {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        
        int i, j, k, row1, column1, row2, column2, sum=0;

        System.out.println("Enter the number of rows of first matrix : ");
        row1 = scanner.nextInt();

        System.out.println("Enter the number of columns of first matrix : ");
        column1 = scanner.nextInt();
        
        //Declaring first matrix
        int matrix1[][] = new int[row1][column1];

        //Reading input for first matrix
        for (i = 0; i < row1; i++) {
            for (j = 0; j < column1; j++) {
                System.out.println("matrix1[" + i + "]" + "[" + j + "] : ");
                matrix1[i][j] = scanner.nextInt();
            }
        }
        
        //Displaying Output for first matrix
        System.out.println("The first matrix is : ");
        for (i = 0; i < row1; i++) {
            for (j = 0; j < column1; j++) {
                System.out.print(matrix1[i][j] + " ");
            }
            System.out.println();
        }

        System.out.println("Enter the number of rows of second matrix : ");
        row2 = scanner.nextInt();

        System.out.println("Enter the number of columns of second matrix : ");
        column2 = scanner.nextInt();

        //Declaring second matrix
        int matrix2[][] = new int[row2][column2];

        //Reading input for second matrix
        System.out.println("Enter the elements of second matrix : ");
        for (i = 0; i < row2; i++) {
            for (j = 0; j < column2; j++) {
                System.out.println("matrix2[" + i + "]" + "[" + j + "] : ");
                matrix2[i][j] = scanner.nextInt();
            }
        }
        
        //Displaying Output for second matrix
        System.out.println("The second matrix is : ");
        for (i = 0; i < row2; i++) {
            for (j = 0; j < column2; j++) {
                System.out.print(matrix2[i][j] + " ");
            }
            System.out.println();
        }

        //Declaring Result matrix after multiplication
        int matrix3[][] = new int[row1][column1];

        if(column1!=row2){
            System.out.println("Matrix cannot be multiplied");
        }
        else{
            for (i = 0; i < row1; i++) {
                for (j = 0; j < column2; j++) {
                    for (k = 0; k < column1; k++) {
                        sum = sum + matrix1[i][k]*matrix2[k][j];
                    }
                    matrix3[i][j] = sum;
                    sum = 0;
                }
            }
            //Displaying Output after multiplication
            System.out.println("The matrix after multiplication is : ");
            for (i = 0; i < row1; i++) {
                for (j = 0; j < column2; j++) {
                    System.out.print(matrix3[i][j] + " ");
                }
                System.out.println();
            }
        }
    }
}
Input:
Enter the number of rows of first matrix : 
2
Enter the number of columns of first matrix : 
3
matrix1[0][0] : 
1
matrix1[0][1] : 
2
matrix1[0][2] : 
3
matrix1[1][0] : 
4
matrix1[1][1] : 
5
matrix1[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 second matrix : 
matrix2[0][0] : 
1
matrix2[0][1] : 
4
matrix2[1][0] : 
2
matrix2[1][1] : 
5
matrix2[2][0] : 
3
matrix2[2][1] : 
6

Output:
The first matrix is : 
1 2 3 
4 5 6
The second matrix is : 
1 4 
2 5 
3 6 
The matrix after multiplication is : 
14 32 
32 77

Related Programs

1) Program to read and display 2D array
2) Program to Transpose a matrix
3) Program to Add two Matrices
4) Program to Find the smallest missing number
5) Program to Search for an Element in an Array
6) Program to Rearrange an array such that arr[i]=i
7) Program to Find missing odd number in first n odd number
8) Program to find maximum and second maximum if elements of array is space separated input
9) Program to Print How Many Numbers Smaller than Current Number
10) Remove Duplicate Elements From Array
Share Me

Leave a Reply