
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Program to multiply two matrices in C++
In this tutorial, we will be discussing a program to multiply two matrices.
For this we will be given with two matrices and our task is to print the product of two those matrices. The only condition is that the number of columns of first matrix should be equal to the number of rows of the second matrix.
Example
#include <iostream> using namespace std; #define N 4 //multiplying the elements of both matrices void calc_product(int mat1[][N], int mat2[][N], int res[][N]){ int i, j, k; for (i = 0; i < N; i++) { for (j = 0; j < N; j++){ res[i][j] = 0; for (k = 0; k < N; k++) res[i][j] += mat1[i][k] * mat2[k][j]; } } } int main(){ int i, j; int res[N][N]; int mat1[N][N] = {{1, 1, 1, 1}, {2, 2, 2, 2}, {3, 3, 3, 3}, {4, 4, 4, 4}}; int mat2[N][N] = {{1, 1, 1, 1}, {2, 2, 2, 2}, {3, 3, 3, 3}, {4, 4, 4, 4}}; calc_product(mat1, mat2, res); cout << "Resultant matrix :\n"; for (i = 0; i < N; i++){ for (j = 0; j < N; j++) cout << res[i][j] << " "; cout << "\n"; } return 0; }
Output
Resultant matrix : 10 10 10 10 20 20 20 20 30 30 30 30 40 40 40 40
- Related Articles
- C# program to multiply two matrices
- Java program to multiply two matrices.
- Python program to multiply two matrices
- Swift Program to Multiply two Matrices Using Multi-dimensional Arrays
- C++ Program to Multiply two Matrices by Passing Matrix to Function
- How to Multiply Two Matrices using Python?
- Golang Program to Multiply two Matrices by Passing Matrix to a Function
- Swift Program to Multiply two Matrices by Passing Matrix to a Function
- How to multiply two matrices by elements in R?
- How to multiply two matrices using pointers in C?
- How to multiply corresponding values from two matrices in R?
- How to multiply two matrices in R if they contain missing values?
- How can Tensorflow be used to multiply two matrices using Python?
- Java program to add two matrices.
- Java program to subtract two matrices.

Advertisements