Program to perform matrix addition, matrix subtraction, matrix multiplication

Programs to perform all the basic Matrix operations | Matrix addition, matrix subtraction, matrix multiplication is discussed here.

  • Matrix Addition

mat1  =  {{1, 2}, {3, 4}}
mat2 = {{1, 2}, {3, 4}}
mat1 + mat2 = {{2, 4}, {6, 8}}

  • Matrix Subtraction

mat1  =  {{1, 2}, {3, 4}}
mat2 = {{1, 2}, {3, 4}}
mat1 – mat2 = {{0, 0}, {0, 0}}

  • Matrix Multiplication

mat1  =  {{1, 2}, {3, 4}}
mat2 = {{1, 2}, {3, 4}}
mat1 * mat2 = {{7, 10}, {15, 22}}

Algorithm to perform matrix addition, matrix subtraction, matrix multiplication

Matrix addition:

  1. Input the order of the matrix.
  2. Input the matrix 1 elements.
  3. Input the matrix 2 elements.
  4. Repeat from i = 0 to m
  5. Repeat from j = 0 to n
  6. mat3[i][j] = mat1[i][j] + mat2[i][j]
  7. Print mat3.

Matrix subtraction:

  1. Input the order of the matrix.
  2. Input the matrix 1 elements.
  3. Input the matrix 2 elements.
  4. Repeat from i = 0 to m
  5. Repeat from j = 0 to n
  6. mat3[i][j] = mat1[i][j] – mat2[i][j]
  7. Print mat3.

Matrix multiplication

  1. Input the order of the matrix1 ( m * n).
  2. Input the order of matrix2 (p * q).
  3. Input the matrix 1 elements.
  4. Input the matrix 2 elements.
  5. Repeat from i = 0 to m
  6. Repeat from j = 0 to q
  7. repeat from k = 0 to p
  8. sum = sum + mat1[c][k] * mat2[k][d];
  9. mat3[c][d] = sum
  10. Print mat3.

Program to perform matrix addition, matrix subtraction, matrix multiplication is given below.

Program to perform matrix operations | Matrix addition

/* C Program to perform matrix operations ,matrix addition, matrix subtraction, matrix multiplication – addition */
#include
int main()
{
//fill your code
int m, n;
scanf(“%d %d”,&m,&n);
int i, j;
int mat1[m][n], mat2[m][n], mat3[m][n];
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
scanf(“%d”,&mat1[i][j]);
}
for(i = 0; i < n; i++)
{
for(j = 0; j < n; j++)
scanf(“%d”,&mat2[i][j]);
}

for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
mat3[i][j] = mat1[i][j] + mat2[i][j];
}
}

for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
printf(“%d “, mat3[i][j]);
printf(“\n”);
}

return 0;
}

/* C++ Program to matrix operations ,matrix addition, matrix subtraction, matrix multiplication – addition */
#include
using namespace std;

int main()
{
//fill your code
int m, n;
cin >> m >> n;
int i, j;
int mat1[m][n], mat2[m][n], mat3[m][n];
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
cin >> mat1[i][j];
}
for(i = 0; i < n; i++)
{
for(j = 0; j < n; j++)
cin >> mat2[i][j];
}

for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
mat3[i][j] = mat1[i][j] + mat2[i][j];
}
}

for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
cout << mat3[i][j] << ” “;
cout << endl;
}

return 0;
}

/* Java Program to perform matrix operations ,matrix addition, matrix subtraction, matrix multiplication – addition */

import java.util.*;
public class Main
{
public static void main(String[] args)
{
int n, m;
Scanner sc = new Scanner(System.in);
System.out.print(“\nEnter the order of the matrix : “);
m = sc.nextInt();
n = sc.nextInt();
int[][] mat1 = new int[m][n];
int[][] mat2 = new int[m][n];
int[][] mat3 = new int[m][n];
System.out.print(“\nInput the matrix 1 elements : “);
int i, j;
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
mat1[i][j] = sc.nextInt();
}

System.out.print(“\nInput the matrix 2 elements : “);
for(i = 0; i < n; i++)
{
for(j = 0; j < n; j++)
mat2[i][j] = sc.nextInt();
}

for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
mat3[i][j] = mat1[i][j] + mat2[i][j];
}
}

System.out.println(“Addition of the two matrices :”);
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
System.out.print(mat3[i][j] + ” “);
System.out.println();
}

}
}

# Python program to perform matrix operations ,matrix addition, matrix subtraction, matrix multiplication – addition */

mat1 = [[1, 2], [3, 4]]
mat2 = [[1, 2], [3, 4]]
mat3 = [[0, 0], [0, 0]]

for i in range(0, 2):
for j in range(0, 2):
mat3[i][j] = mat1[i][j] + mat2[i][j]

print(“Addition of two matrices”)
for i in range(0, 2):
for j in range(0, 2):
print(mat3[i][j], end = ” “)
print()

Output:

matrix addition, matrix subtraction, matrix multiplication - matrix addition

Program to perform basic matrix operations | Matrix Subtraction

/* C Program to perform matrix operations ,matrix addition, matrix subtraction, matrix multiplication – subtraction */
#include
int main()
{
int m, n;
scanf(“%d %d”,&m,&n);
int i, j;
int mat1[m][n], mat2[m][n], mat3[m][n];
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
scanf(“%d”,&mat1[i][j]);
}
for(i = 0; i < n; i++)
{
for(j = 0; j < n; j++)
scanf(“%d”,&mat2[i][j]);
}

for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
mat3[i][j] = mat1[i][j] – mat2[i][j];
}
}

for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
printf(“%d “, mat3[i][j]);
printf(“\n”);
}

return 0;
}

/* C++ Program to perform matrix operations ,matrix addition, matrix subtraction, matrix multiplication – subtraction */
#include
using namespace std;

int main()
{
int m, n;
cin >> m >> n;
int i, j;
int mat1[m][n], mat2[m][n], mat3[m][n];
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
cin >> mat1[i][j];
}
for(i = 0; i < n; i++)
{
for(j = 0; j < n; j++)
cin >> mat2[i][j];
}

for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
mat3[i][j] = mat1[i][j] – mat2[i][j];
}
}

for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
cout << mat3[i][j] << ” “;
cout << endl;
}

return 0;
}

/* Java Program to perform matrix operations ,matrix addition, matrix subtraction, matrix multiplication – subtraction */

import java.util.*;
public class Main
{
public static void main(String[] args)
{
int n, m;
Scanner sc = new Scanner(System.in);
System.out.print(“\nEnter the order of the matrix : “);
m = sc.nextInt();
n = sc.nextInt();
int[][] mat1 = new int[m][n];
int[][] mat2 = new int[m][n];
int[][] mat3 = new int[m][n];
System.out.print(“\nInput the matrix 1 elements : “);
int i, j;
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
mat1[i][j] = sc.nextInt();
}

System.out.print(“\nInput the matrix 2 elements : “);
for(i = 0; i < n; i++)
{
for(j = 0; j < n; j++)
mat2[i][j] = sc.nextInt();
}

for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
mat3[i][j] = mat1[i][j] – mat2[i][j];
}
}

System.out.println(“Subtraction of the two matrices :”);
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
System.out.print(mat3[i][j] + ” “);
System.out.println();
}

}
}

# Python program to perform matrix operations ,matrix addition, matrix subtraction, matrix multiplication – subtraction 

mat1 = [[10, 9], [8, 6]]
mat2 = [[1, 2], [3, 4]]
mat3 = [[0, 0], [0, 0]]

for i in range(0, 2):
for j in range(0, 2):
mat3[i][j] = mat1[i][j] – mat2[i][j]

print(“Subtraction of two matrices”)
for i in range(0, 2):
for j in range(0, 2):
print(mat3[i][j], end = ” “)
print()

Output:

matrix addition, matrix subtraction, matrix multiplication - matrix subtraction

Program to perform basic matrix operations | Matrix multiplication

/* C program to multiply matrices */
#include <stdio.h>

int main()
{
int m, n, p, q, c, d, k, sum = 0;
int mat1[10][10], mat2[10][10], mat3[10][10];

printf(“Enter number of rows and columns of mat1 matrix\n”);
scanf(“%d%d”, &m, &n);
printf(“Enter elements of matrix 1\n”);

for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
scanf(“%d”, &mat1[c][d]);

printf(“\nEnter number of rows and columns of mat2 matrix\n”);
scanf(“%d%d”, &p, &q);

if (n != p)
printf(“\nThe matrices can’t be multiplied with each other.\n”);
else
{
printf(“\nEnter elements of matrix2\n”);

for (c = 0; c < p; c++)
for (d = 0; d < q; d++)
scanf(“%d”, &mat2[c][d]);

for (c = 0; c < m; c++) {
for (d = 0; d < q; d++) {
for (k = 0; k < p; k++) {
sum = sum + mat1[c][k]*mat2[k][d];
}

mat3[c][d] = sum;
sum = 0;
}
}

printf(“\nProduct of the matrices:\n”);

for (c = 0; c < m; c++) {
for (d = 0; d < q; d++)
printf(“%d\t”, mat3[c][d]);

printf(“\n”);
}
}

return 0;
}

/* C++ program to multiply matrices */
#include <iostream>
using namespace std;

int main()
{
int m, n, p, q, c, d, k, sum = 0;
int mat1[10][10], mat2[10][10], mat3[10][10];

cout << “Enter number of rows and columns of matrix1\n”;
cin >> m >> n;
cout << “Enter elements of matrix 1\n”;

for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
cin >> mat1[c][d];

cout << “\nEnter number of rows and columns of matrix2\n”;
cin >> p >> q;

if (n != p)
cout << “\nThe matrices can’t be multiplied with each other.\n”;
else
{
cout << “\nEnter elements of matrix2\n”;

for (c = 0; c < p; c++)
for (d = 0; d < q; d++)
cin >> mat2[c][d];

for (c = 0; c < m; c++) {
for (d = 0; d < q; d++) {
for (k = 0; k < p; k++) {
sum = sum + mat1[c][k]*mat2[k][d];
}

mat3[c][d] = sum;
sum = 0;
}
}

cout << “\nProduct of the matrices:\n”;

for (c = 0; c < m; c++) {
for (d = 0; d < q; d++)
cout << mat3[c][d] << ” “;

cout << endl;
}
}

return 0;
}

/* Java program to multiply two matrices */
import java.util.Scanner;

class Main
{
public static void main(String args[])
{
int m, n, p, q, sum = 0, c, d, k;

Scanner in = new Scanner(System.in);
System.out.println(“Enter the number of rows and columns of first matrix”);
m = in.nextInt();
n = in.nextInt();

int first[][] = new int[m][n];

System.out.println(“Enter elements of first matrix”);

for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
first[c][d] = in.nextInt();

System.out.println(“Enter the number of rows and columns of second matrix”);
p = in.nextInt();
q = in.nextInt();

if (n != p)
System.out.println(“The matrices can’t be multiplied with each other.”);
else
{
int second[][] = new int[p][q];
int multiply[][] = new int[m][q];

System.out.println(“Enter elements of second matrix”);

for (c = 0; c < p; c++)
for (d = 0; d < q; d++)
second[c][d] = in.nextInt();

for (c = 0; c < m; c++)
{
for (d = 0; d < q; d++)
{
for (k = 0; k < p; k++)
{
sum = sum + first[c][k]*second[k][d];
}

multiply[c][d] = sum;
sum = 0;
}
}

System.out.println(“Product of the matrices:”);

for (c = 0; c < m; c++)
{
for (d = 0; d < q; d++)
System.out.print(multiply[c][d]+”\t”);

System.out.print(“\n”);
}
}
}
}

# Python program to multiply two matrices

mat1 = [[10, 9], [8, 6]]
mat2 = [[1, 2], [3, 4]]
mat3 = [[0, 0], [0, 0]]

for i in range(0, 2):
for j in range(0, 2):
mat3[i][j] = mat1[i][j] – mat2[i][j]

print(“Multiplication of two matrices”)
for i in range(len(mat1)):
for j in range(len(mat2[0])):
for k in range(len(mat2)):
mat3[i][j] =mat3[i][j] + ( mat1[i][k] * mat2[k][j])

for r in mat3:
print(r)

Output:

matrix addition, matrix subtraction, matrix multiplication - matrix multiplication