C program to subtract two matrices

Write a C program to read elements in two matrices and find the difference of two matrices. Program to subtract two matrices in C. Logic to subtract two matrices in C programming.

Required knowledge

Basic C programming, C for loop, ArrayS

Matrix Subtraction

Elements of two matrices can only be subtracted if and only if both matrices are of same size. Matrix subtraction is done element wise (entry wise) i.e. Difference of two matrices A and B of size mXn is defined by

A - B = Aij - Bij (Where 1 ≤ i ≤ m and 1 ≤ j ≤ n)

Matrix subtraction

Program to subtract two matrices





/**
 * C program to find difference of two matrices of size 3x3
 */

#include <stdio.h>

#define SIZE 3 // Size of the matrix

int main()
{
    int A[SIZE][SIZE];  // Matrix 1
    int B[SIZE][SIZE];  // Matrix 2
    int C[SIZE][SIZE];  // Resultant matrix

    int row, col;

    /* Input elements in first matrix */
    printf("Enter elements in matrix A of size 3x3: \n");
    for(row=0; row<SIZE; row++)
    {
        for(col=0; col<SIZE; col++)
        {
            scanf("%d", &A[row][col]);
        }
    }

    /* Input elements in second matrix */
    printf("\nEnter elements in matrix B of size 3x3: \n");
    for(row=0; row<SIZE; row++)
    {
        for(col=0; col<SIZE; col++)
        {
            scanf("%d", &B[row][col]);
        }
    }

    /*
     * Subtract both matrices and store the result in matrix C
     */
    for(row=0; row<SIZE; row++)
    {
        for(col=0; col<SIZE; col++)
        {
            /* Cij = Aij - Bij */
            C[row][col] = A[row][col] - B[row][col];
        }
    }

    /* Print difference of both matrices A and B */

    printf("\nDifference of two matrices A-B = \n");
    for(row=0; row<SIZE; row++)
    {
        for(col=0; col<SIZE; col++)
        {
            printf("%d ", C[row][col]);
        }
        printf("\n");
    }

    return 0;
}



Output:

Enter elements in matrix A of size 3x3:
1 2 3
4 5 6
7 8 9
Enter elements in matrix B of size 3x3:
4 5 6
7 8 9
3 2 1
Difference of two matrices A-B =
-3 -3 -3
-3 -3 -3
 4  6  8
Process returned 0 (0x0) execution time : 33.774 s

C program to find sum of two matrices





/**
 * C program to find sum of two matrices 
 */

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int A[10][10]; // Matrix 1
    int B[10][10]; // Matrix 2
    int C[10][10]; // Resultant matrix
    int row, col,m,n;
    // int A[10][10],B[10][10],C[10][10];
    printf("Enter no of rows:");
    scanf("%d",&m);
    printf("Enter no of columns:");
    scanf("%d",&n);
    /* Input elements in first matrix*/
    printf("Enter elements in matrix A \n");
    for(row=0; row<m; row++)
    {
        for(col=0; col<n; col++)
        {
            scanf("%d", &A[row][col]);
        }
    }

    /* Input elements in second matrix */
    printf("\nEnter elements in matrix B : \n");
    for(row=0; row<m; row++)
    {
        for(col=0; col<n; col++)
        {
            scanf("%d", &B[row][col]);
        }
    }

    /*
     * Sub tract both matrices A and B entry wise or element wise
     * and stores result in matrix C
     */
    for(row=0; row<m; row++)
    {
        for(col=0; col<n; col++)
        {
            /* Cij = Aij + Bij */
            C[row][col] = A[row][col] - B[row][col];
        }
    }


    /* Print the value of resultant matrix C */
    printf("\nSum of matrices A-B = \n");
    for(row=0; row<m; row++)
    {
        for(col=0; col<n; col++)
        {
            printf("%d ", C[row][col]);
        }
        printf("\n");
    }

    return 0;
}



Output:

Enter no of rows:3
Enter no of columns:3
Enter elements in matrix A of size 3x3:
2 8 5
8 1 7
3 0 4
Enter elements in matrix B of size 3x3:
9 1 4
8 4 2
7 5 3
Sum of matrices A-B =
-7  7 1
 0 -3 5
-4 -5 1
Process returned 0 (0x0) execution time : 75.994 s



Instagram