1.
Matrix Addition Program (2D Array)
#include <iostream>
using namespace std;
int main() {
int a[2][2], b[2][2], sum[2][2];
// Input elements for matrix A
cout << "Enter elements of first 2x2 matrix:\n";
for(int i = 0; i < 2; i++) {
for(int j = 0; j < 2; j++) {
cin >> a[i][j];
// Input elements for matrix B
cout << "Enter elements of second 2x2 matrix:\n";
for(int i = 0; i < 2; i++) {
for(int j = 0; j < 2; j++) {
cin >> b[i][j];
// Adding matrices
for(int i = 0; i < 2; i++) {
for(int j = 0; j < 2; j++) {
sum[i][j] = a[i][j] + b[i][j];
// Display result
cout << "Sum of the two matrices:\n";
for(int i = 0; i < 2; i++) {
for(int j = 0; j < 2; j++) {
cout << sum[i][j] << " ";
}
cout << endl;
return 0;
✅ 2. Matrix Multiplication Program (2D Array)
cpp
CopyEdit
#include <iostream>
using namespace std;
int main() {
int a[2][2], b[2][2], product[2][2];
// Input matrices
cout << "Enter elements of first 2x2 matrix:\n";
for(int i = 0; i < 2; i++) {
for(int j = 0; j < 2; j++) {
cin >> a[i][j];
cout << "Enter elements of second 2x2 matrix:\n";
for(int i = 0; i < 2; i++) {
for(int j = 0; j < 2; j++) {
cin >> b[i][j];
// Initialize result matrix to 0
for(int i = 0; i < 2; i++) {
for(int j = 0; j < 2; j++) {
product[i][j] = 0;
}
// Multiply matrices
for(int i = 0; i < 2; i++) {
for(int j = 0; j < 2; j++) {
for(int k = 0; k < 2; k++) {
product[i][j] += a[i][k] * b[k][j];
// Display result
cout << "Product of the two matrices:\n";
for(int i = 0; i < 2; i++) {
for(int j = 0; j < 2; j++) {
cout << product[i][j] << " ";
cout << endl;
return 0;
✅ Dynamic Size Matrix Addition and Multiplication (C++)
cpp
CopyEdit
#include <iostream>
using namespace std;
int main() {
int rows, cols;
cout << "Enter number of rows: ";
cin >> rows;
cout << "Enter number of columns: ";
cin >> cols;
int a[100][100], b[100][100], sum[100][100], product[100][100];
// Input first matrix
cout << "\nEnter elements of Matrix A:\n";
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
cin >> a[i][j];
// Input second matrix
cout << "\nEnter elements of Matrix B:\n";
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
cin >> b[i][j];
// Matrix Addition
cout << "\nMatrix Addition (A + B):\n";
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
sum[i][j] = a[i][j] + b[i][j];
cout << sum[i][j] << " ";
cout << endl;
// Matrix Multiplication only if columns of A = rows of B
if (rows == cols) {
// Initialize product to 0
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
product[i][j] = 0;
// Matrix Multiplication
cout << "\nMatrix Multiplication (A x B):\n";
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
for(int k = 0; k < cols; k++) {
product[i][j] += a[i][k] * b[k][j];
cout << product[i][j] << " ";
cout << endl;
} else {
cout << "\nMatrix multiplication not possible (columns of A ≠ rows of B).\n";
return 0;
✅ Matrix Multiplication with Different Sizes (m x n × n x p)
#include <iostream>
using namespace std;
int main() {
int m, n, p;
// Input sizes
cout << "Enter rows and columns of Matrix A (m x n): ";
cin >> m >> n;
cout << "Enter number of columns of Matrix B (n x p): ";
cin >> p;
int A[100][100], B[100][100], result[100][100];
// Input Matrix A
cout << "\nEnter elements of Matrix A:\n";
for(int i = 0; i < m; i++) {
for(int j = 0; j < n; j++) {
cin >> A[i][j];
// Input Matrix B
cout << "\nEnter elements of Matrix B:\n";
for(int i = 0; i < n; i++) {
for(int j = 0; j < p; j++) {
cin >> B[i][j];
// Initialize result matrix with 0
for(int i = 0; i < m; i++) {
for(int j = 0; j < p; j++) {
result[i][j] = 0;
// Multiply A × B = result
for(int i = 0; i < m; i++) {
for(int j = 0; j < p; j++) {
for(int k = 0; k < n; k++) {
result[i][j] += A[i][k] * B[k][j];
}
// Display result
cout << "\nProduct of Matrix A and Matrix B:\n";
for(int i = 0; i < m; i++) {
for(int j = 0; j < p; j++) {
cout << result[i][j] << " ";
cout << endl;
return 0;