Skip to main content

Program for Matrix Multiplication in C++, Python, Java

Program for Matrix Multiplication

Matrix multiplication is a fundamental operation in linear algebra and is used in many areas of computer science, including machine learning, computer graphics, and scientific computing. In coding, matrix multiplication is typically performed using loops or libraries that implement efficient algorithms.






Program for Matrix Multiplication in C++  


#include <iostream>

using namespace std;


int main()

{   int r1,r2,c1,c2;

    int i,j,k;

    int mat[5][5],mat2[5][5],multi[6][5];

    cout<<"Rows 1 : "<<endl;

    cin>>r1;

    cout<<"Col 1 : "<<endl;

    cin>>c1;

    cout<<"Rows 2 : "<<endl;

    cin>>r2;

    cout<<"Col 2 : "<<endl;

    cin>>c2;

    cout<<"Enter data for matrix 1"<<endl;

    for (i=0;i<r1;i++){

        for(j=0;j<c1;j++){

            cin>>mat[i][j];

        }

    }

    cout<<"Enter data for matrix 2"<<endl;

    for (i=0;i<r2;i++){

        for(j=0;j<c2;j++){

            cin>>mat2[i][j];

        }

    }

    if(c1==r2){

        cout<<"Multiplication is possible."<<endl;

        for (i=0;i<r1;i++){

            for(j=0;j<c2;j++){

                multi[i][j]=0;

                for(k=0;k<c1;k++){

                    multi[i][j]= multi[i][j]+(mat[i][k]*mat2[k][j]);

                }

                cout<<multi[i][j]<<" ";

            }

            cout<<endl;

        }    

    }

    else { cout<< "Multiplication not possible !"<<endl;}

    return 0;

}


Output:- 

Rows 1 : 

3  

Col 1 : 

3

Rows 2 : 

3

Col 2 : 

3

Enter data for matrix 1

1 2 3        

4 5 6

7 8 9

Enter data for matrix 2

9 8 7

6 5 4

3 2 1

Multiplication is possible.

30 24 18 

84 69 54 

138 114 90 


Program for Matrix Multiplication in Python

r1 = int(input("Rows 1 : "))

c1 = int(input("Col 1 : "))

r2 = int(input("Rows 2 : "))

c2 = int(input("Col 2 : "))


mat = [[0 for j in range(c1)] for i in range(r1)]

mat2 = [[0 for j in range(c2)] for i in range(r2)]

multi = [[0 for j in range(c2)] for i in range(r1)]


print("Enter data for matrix 1")

for i in range(r1):

    for j in range(c1):

        mat[i][j] = int(input())


print("Enter data for matrix 2")

for i in range(r2):

    for j in range(c2):

        mat2[i][j] = int(input())


if c1 == r2:

    print("Multiplication is possible.")

    for i in range(r1):

        for j in range(c2):

            multi[i][j] = 0

            for k in range(c1):

                multi[i][j] = multi[i][j] + (mat[i][k] * mat2[k][j])

            print(multi[i][j], end=" ")

        print()

else:

    print("Multiplication not possible !")


Output:- 

Rows 1 : 

3  

Col 1 : 

3

Rows 2 : 

3

Col 2 : 

3

Enter data for matrix 1

1 2 3        

4 5 6

7 8 9

Enter data for matrix 2

9 8 7

6 5 4

3 2 1

Multiplication is possible.

30 24 18 

84 69 54 

138 114 90 


Program for Matrix Multiplication in Java  

import java.util.Scanner;


public class Main {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        int r1, r2, c1, c2, i, j, k;


        System.out.println("Rows 1: ");

        r1 = input.nextInt();

        System.out.println("Col 1: ");

        c1 = input.nextInt();

        System.out.println("Rows 2: ");

        r2 = input.nextInt();

        System.out.println("Col 2: ");

        c2 = input.nextInt();


        int[][] mat = new int[5][5];

        int[][] mat2 = new int[5][5];

        int[][] multi = new int[6][5];


        System.out.println("Enter data for matrix 1");

        for (i = 0; i < r1; i++) {

            for (j = 0; j < c1; j++) {

                mat[i][j] = input.nextInt();

            }

        }

        System.out.println("Enter data for matrix 2");

        for (i = 0; i < r2; i++) {

            for (j = 0; j < c2; j++) {

                mat2[i][j] = input.nextInt();

            }

        }


        if (c1 == r2) {

            System.out.println("Multiplication is possible.");

            for (i = 0; i < r1; i++) {

                for (j = 0; j < c2; j++) {

                    multi[i][j] = 0;

                    for (k = 0; k < c1; k++) {

                        multi[i][j] = multi[i][j] + (mat[i][k] * mat2[k][j]);

                    }

                    System.out.print(multi[i][j] + " ");

                }

                System.out.println();

            }

        } else {

            System.out.println("Multiplication not possible !");

        }

    }

}


Output:- 

Rows 1 : 

3  

Col 1 : 

3

Rows 2 : 

3

Col 2 : 

3

Enter data for matrix 1

1 2 3        

4 5 6

7 8 9

Enter data for matrix 2

9 8 7

6 5 4

3 2 1

Multiplication is possible.

30 24 18 

84 69 54 

138 114 90 


Comments

Popular posts from this blog

TechGig Code Gladiators 2023 Coding Solutions

TechGig Open Coding Round Code Gladiators 2023 Answers Round 1  - Problem 1 Forest Fire Coding Solution Figure out the minimum energy level P such that they can get exactly X animals to transport.  Test Case: 5 4  1 3 2 4 5 Output: 2 Coding Solution in Python 3: from bisect import bisect_left #Telegram - @PLACEMENTLELO n, m = map ( int , input ().split()) a = list ( map ( int , input ().split())) a.sort() #Telegram - @PLACEMENTLELO if n - bisect_left(a, a[n-m]) != m: print ( -1 ) else : print (a[n - m]) #Telegram - @PLACEMENTLELO ------------------------------------------- Round 2  - Problem 2 The Magic Coding Solution The cost of using wand for operation on element equal to absolute difference between value of element and desired value after operation. Test Case: 5 3 1 2 3 4 5 5 2 1 Output: 10 7 10 Coding Solution in Python 3: from bisect import bisect_left #Telegram - @PLACEMENTLELO n, m = map ( int , input ().split()) A = list ( map ( int , input ()...

The Divisibility Dilemma Code Techgig Geek Goddess Solution

The Divisibility Dilemma Code Techgig Geek Goddess Solution 100% Working Code uploaded on Youtube: https://youtu.be/6cn7nNtSiKo 100% Working Code uploaded on Telegram: https://telegram.me/PLACEMENTLELO The Divisibility Dilemma (100 Marks) In the picturesque village of Numeropolis, the annual "Divisibility Day" is celebrated with great enthusiasm. This day is marked by various mathematical challenges and games that put the villagers' arithmetic skills to the test. This year, the villagers are embarking on a quest to explore the distinctive properties of subarrays within integer arrays. The village's renowned mathematician, Professor Arithmo, has crafted an intriguing challenge. He has provided the villagers with an integer array nums`and two additional integers, K and P. Professor Arithmo's challenge revolves around discovering and counting distinct subarrays from the array nums. The uniqueness of these subarrays is determined by their divisibility characteristics....

Program for Oscillating Prices of Chakri Coding Problem

Oscillating Prices of Chakri Diwali is here. While everyone here is busy texting "Happy Diwali" wishes to everybody else, Coder has some other plans and wants to earn some money this season. Now, the Apex court has allowed the sale of only green crackers this Diwali. Out of all green crackers, "Chakri" is most popular. Because of the irregular supply of "Chakri", the price of "Chakri" is oscillating daily. Coder saw a business opportunity in this. He/She got a price list for coming N days from an insider in the market union. Prices in the list are for 1 unit of a large packet of "Chakri". Each large packet contains 100 units of Chakri. Now, due to financial limitations, Coder can transact only 1 large packet (100 units of "Chakri") in the market. You have to tell maximum profit possible, given that he/she can transact atmost one time. Note: 1. Transaction refers to the act of buying and selling.       2. "Chakri" can...