Home » Programming » C# Program to perform matrix subtraction between two matrices.

# C# Program to perform matrix subtraction between two matrices. In mathematics, matrix subtraction is that the operation of subtracting 2 matrices by subtracting the corresponding entries along. In this example, we’ll demonstrate C# program for subtraction of two matrices. For this operation, obviously we need two matrices to perform the subtraction.

## Main idea behind the matrix subtraction.

Two matrices must have an equal number of rows & columns to be subtracted. The difference of two matrices A and B are going to be a matrix which has an equivalent number of rows and columns as A and B. The sum of A and B, denoted A – B, is computed by subtracting corresponding entries of A and B.
Or, C = A – B

## Algorithm for matrix subtraction between two matrices.

```Step 1: Start
Step 2: Input the size of the matrix  (i.e. number of rows and number of column).
Step 3: Input the elements of the of first matrix.
Step 4: Input the elements of the of second matrix.
Step 5: Subtracting the corresponding entries of the first matrix and second matrix.```

## Program for matrix subtraction between two matrices.

```using System;

namespace MatrixSubtraction
{
class Program
{
static void Main(string[] args)
{
int i,j,r,c;
int[,] Mat1 = new int[100,100];
int[,] Mat2 = new int[100,100];
int[,] Mat3 = new int[100,100];

Console.Write("Subtraction of two matrices :\n\n");
Console.Write("Size of the matrix? (<= 10): \n");
Console.Write("Row: ");
Console.Write("Column: ");

Console.Write("\nEnter the elements of the 1st matrix :\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
Console.Write("Element - [{0},{1}] : ",i,j);
}
}

Console.Write("\nEnter the elements of the 2nd matrix :\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
Console.Write("Element - [{0},{1}] : ",i,j);
}
}
Console.Write("\nThe 1st matrix is :");

for(i=0;i<r;i++)
{
Console.Write("\n");
for(j=0;j<c;j++)
Console.Write("{0}\t",Mat1[i,j]);
}
Console.Write("\n");

Console.Write("\nThe 2nd matrix is :");

for(i=0;i<r;i++)
{
Console.Write("\n");
for(j=0;j<c;j++)
Console.Write("{0}\t",Mat2[i,j]);
}
Console.Write("\n");

for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
Mat3[i,j]=Mat1[i,j]-Mat2[i,j];
}
}

Console.Write("\nThe subtraction of two matrix is :");
for(i=0;i<r;i++)
{
Console.Write("\n");
for(j=0;j<c;j++)
Console.Write("{0}\t",Mat3[i,j]);
}
Console.Write("\n\n");
}
}
}```

### Explanation

• At first, we declare the variables and array.
```int i,j,r,c;
int[,] Mat1 = new int[100,100];
int[,] Mat2 = new int[100,100];
int[,] Mat3 = new int[100,100];```

Variables i & j are for looping, r & c are for storing number of rows and columns respectively and Mat1Mat2 & Mat3 are the arrays for storing the elements of the first, second and resultant matrix respectively.

• Then, we need to input the size of the matrix.
```Console.Write("Row: ");
Console.Write("Column: ");

Basically, this block of code takes the size of matrix in the form of rows and columns. The value for the number of rows is stored in variable r, where as the value for the number of columns is stored in variable c.

• Now, we need to input the elements of the two matrices for performing the addition between them.
```for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
Console.Write("Element - [{0},{1}] : ",i,j);
}
}```

Above block of code takes the elements of the first matrix and stores in the array Mat1.

```for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
Console.Write("Element - [{0},{1}] : ",i,j);
}
}```

Similary, the above block of code takes the elements of the second matrix and stores in the array Mat2.

• Important part of the program is to perform the subtraction between the given two matrices.
```for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
Mat3[i,j]=Mat1[i,j]-Mat2[i,j];
}
}```

The above block of code performs the subtraction operation between the two matrices provided by the user and stores the values in the array Mat3.

• Finally, the elements of the array Mat3 in the matrix form is the required output.

More Examples