Q. Write an algorithm and program to subtract two matrices.

Solution :In this program, you'll learn to subtract two matrices using Nested loop and display it using Nested loop.

Matrices:- A two dimensional(2D) array is known as Matrix. It can have m number of rows and n number of columns where m and n are two integer values.

Matrix Subtraction algorithm

//Algorithm to Subtract Two Matrices.
START
Step 1 ->  Input matrix 1 and matrix 2.
Step 2 ->  If the number of rows and number of columns of matrix 1 and matrix 2 are equal then execute step 3 else subtraction not possible
Step 3 ->  for i=1 to rows[matrix 1]
for j=1 to columns [matrix 1]
Input matrix 1 [i,j]
Input matrix 2 [i,j]
matrix 3 [i,j]= matrix 1 [i,j] - matrix 2 [i,j];
step 4->   Display matrix 3 [i,j];
STOP


Matrices Subtraction Program

  • C
  • C++
  • Java
  • Python
  • C#
  • PHP
  • //Program to subtract Two Matrices In C.
    #include <stdio.h>
    int main()
    {
    int mat1[2][2], mat2[2][2], sub[2][2], i, j;
    for(i=0;i<2;i++)
    {
    for(j=0;j<2;j++)
    {
    scanf("%d",&mat1[i][j]);
    scanf("%d",&mat2[i][j]);
    sub[i][j]=mat1[i][j]-mat2[i][j];
    }
    }
    for(i=0;i<2;i++)
    {
    for(j=0;j<2;j++)
    {printf("%d\t",sub[i][j]);
    }
    printf("\n");
    }
    return 0;
    }
    
    //Program to subtract Two Matrices In C++.
    #include <iostream>
    using namespace std;
    int main()
    {
    int mat1[2][2], mat2[2][2], sub[2][2], i, j;
    for(i=0;i<2;i++)
    {
    for(j=0;j<2;j++)
    {
    cin>>mat1[i][j];
    cin>>mat2[i][j];
    sub[i][j]=mat1[i][j]-mat2[i][j];
    }
    }
    for(i=0;i<2;i++)
    {
    for(j=0;j<2;j++)
    {cout<<sub[i][j]<<"\t";
    }
    printf("\n");
    }
    return 0;
    }
    
    //Program to Subtract Two Matrices In Java.
    import java.util.*;
    public class Main
    {
    public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int mat1[][] = new int[2][2];
    int mat2[][] = new int[2][2];
    int sub[][] = new int[2][2];
    int i,j;
    for (i = 0; i < 2; i++)
    {
    for (j = 0; j < 2; j++)
    {
    mat1[i][j] = in.nextInt();
    mat2[i][j] = in.nextInt();
    sub[i][j]=mat1[i][j]-mat2[i][j];
    }
    }
    for (i = 0; i < 2; i++)
    {
    for (j = 0; j < 2; j++)
    {
    System.out.print(sub[i][j]+"\t");
    }
    System.out.println();
    }
    }
    }
    
    //Program to Subtract Two Matrices In Python.
    mat1 = [[2,4],
    [6,8]]
    mat2 = [[1,3],
    [5,7]]
    sub = [[0,0],
    [0,0]]
    for i in range(len(mat1)):
    for j in range(len(mat1[0])):
    sub[i][j] = mat1[i][j] - mat2[i][j]
    for r in sub:
    print(r)
    
    //Program to Subtract Two Matrices In C#.
    using System;
    public class LFC {
    public static void Main() {
    int i, j;
    int[, ] mat1 = new int[2, 2];
    int[, ] mat2 = new int[2, 2];
    int[, ] sub = new int[2, 2];
    for (i = 0; i < 2; i++) {
    for (j = 0; j < 2; j++)
    {
    mat1[i, j] = Convert.ToInt32(Console.ReadLine());
    mat2[i, j] = Convert.ToInt32(Console.ReadLine());
    }
    }
    for (i = 0; i < 2; i++)
    for (j = 0; j < 2; j++)
    sub[i, j] = mat1[i, j] - mat2[i, j];
    for (i = 0; i < 2; i++) {
    Console.Write("\n");
    for (j = 0; j < 2; j++)
    Console.Write("{0}\t", sub[i, j]);
    }
    Console.Write("\n\n");
    }
    }
    //Program to Subtract Two Matrices In PHP.
    <?php
    $mat1=array(array());
    $mat2=array(array());
    $sub=array(array());
    $m=2;
    $n=1;
    for($i=0;$i<2;$i=$i+1)
    {
    for($j=0;$j<2;$j=$j+1)
    {
    $mat1[$i][$j]=$m;
    echo $mat1[$i][$j]." ";
    $m=$m+2;
    $mat2[$i][$j]=$n;
    echo $mat2[$i][$j]." ";
    $n=$n+2;
    }
    }
    echo"<br/>";
    for($i=0;$i<2;$i=$i+1)
    {
    for($j=0;$j<2;$j=$j+1)
    {
    $sub[$i][$j]=$mat1[$i][$j]-$mat2[$i][$j]; 
    echo $sub[$i][$j]." ";
    }
    }
    ?>
    //Program to Subtract Two Matrices In C.
    #include <stdio.h>
    int main()
    {
    int mat1[2][2], mat2[2][2], sub[2][2], i, j;
    for(i=0;i<2;i++)
    {
    for(j=0;j<2;j++)
    {
    scanf("%d",&mat1[i][j]);
    scanf("%d",&mat2[i][j]);
    sub[i][j]=mat1[i][j]-mat2[i][j];
    }
    }
    for(i=0;i<2;i++)
    {
    for(j=0;j<2;j++)
    {printf("%d\t",sub[i][j]);
    }
    printf("\n");
    }
    return 0;
    }
    

    Output

    mat1   mat2
    3      2
    6      4
    9      6
    9      8
    sum matrix :
    1   2
    3   1
    

    Recommended Programs

       Program to add two matrices.
       Program to check Armstrong number or not.