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

## Main idea behind the matrix addition.

Two matrices must have an equal number of rows & columns to be added. The sum 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 adding corresponding entries of **A** and **B**.

Or, **A+B=C**

## Algorithm for matrix addition 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: Adding the corresponding entries of the first matrix and second matrix.

*Also Read:*

C# Program to perform matrix subtraction between two matrices.

## Program for matrix addition between two matrices.

using System; namespace MatrixAddition { 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("Addition of two matrices :\n\n"); Console.Write("Size of the matrix? (<= 10): \n"); Console.Write("Row: "); r = Convert.ToInt32(Console.ReadLine()); Console.Write("Column: "); c = Convert.ToInt32(Console.ReadLine()); 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); Mat1[i,j] = Convert.ToInt32(Console.ReadLine()); } } 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); Mat2[i,j] = Convert.ToInt32(Console.ReadLine()); } } 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 addition 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"); } } }

### Output

### 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 **Mat1**, **Mat2** & **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: "); r = Convert.ToInt32(Console.ReadLine()); Console.Write("Column: "); c = Convert.ToInt32(Console.ReadLine());

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); Mat1[i,j] = Convert.ToInt32(Console.ReadLine()); } }

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); Mat2[i,j] = Convert.ToInt32(Console.ReadLine()); } }

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 addition 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 addition 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**