# C program for matrix addition

devquora

Posted On: Jan 27, 2023

### Overview:

A matrix is a rectangular array or table of numbers, symbols, or expressions, arranged in rows and columns. In this article, I will introduce you to the mathematical concept of Matrix and matrix Addition. After that, I will explain What is an array? and What is a Multidimensional Array? Moreover, I will show a Demonstration of the addition of matrices. At last, I will show a C program to add two matrices with output in front of you.

### What is Matrix?

A matrix is a rectangular array or table of numbers, symbols, or expressions, arranged in rows and columns. The matrix of r rows and c columns is known as the r X c matrix. Matrices are often denoted using capital roman letters such as A, B, and C. For example,

Matrix addition is the operation of adding two matrices by adding the corresponding entries together. Matrix addition is done element-wise (entry-wise) i.e. Sum of two matrices. A and B of size mXn is defined by:

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

### What is an Array?

An array is a container for the collection of items that stores these items at contiguous memory locations. An array is a variable that can store multiple values. For example, if you want to store 100 integers in a single variable then you can create an array for it.

### What is a multi-dimensional array?

A multidimensional array is an array of arrays. You can declare a two-dimensional (2d) array. For example, float x[3][4]; Similarly, you can declare a three-dimensional (3d) array. For example, float y[2][4][3];

### The logic for matrix addition

To add two matrices, i.e., compute their sum and print it. A user inputs their orders (number of rows and columns) and the elements of these two matrices. For example, if the order is 2, 2, i.e., two rows and two columns, and the matrices are as follows:

First matrix A =

1 1 2

3 1 4

Second matrix B=

4  1 5

-1 5 3

The Sum of these two matrices is:

1+4 1+1 2+5

3-1 1+5 4+3

=

5 2 7

2 6 7

The main logic to write a C program for the Matrix Addition is as follows:

• Step 1: Take 3 arrays such as first[][], second[][], and sum[][].
• Step 2: Accept the number of rows and columns for the two arrays.
• Step 3: Then, accept elements of these two arrays.
• Step 4: After all, add the corresponding elements of the arrays and store its elements to the new array and display their elements.

### C program for matrix addition

```#include <stdio.h>

int main()
{
int r, c, i, j, first[10][10], second[10][10], sum[10][10];

printf("Enter the number of rows and columns of matrix\n");
scanf("%d%d", &r, &c);
printf("Enter the elements of first matrix\n");

for (i = 0; i < r; i++)
for (j = 0; j < c; j++)
scanf("%d", &first[i][j]);

printf("Enter the elements of second matrix\n");

for (i = 0; i < r; i++)
for (j = 0 ; j < c; j++)
scanf("%d", &second[i][j]);

printf("Sum of entered matrices:-\n");

for (i = 0; i < r; i++) {
for (j = 0 ; j < c; j++) {
sum[i][j] = first[i][j] + second[i][j];
printf("%d\t", sum[i][j]);
}
printf("\n");
}

return 0;
}

```

Conclusion:

This Program shows matrix addition through the addition of corresponding elements of the matrices.

Never Miss an Articles from us.

## Related Articles

#### C Program to convert Decimal to Binary Number

A Decimal Number is constructed with any digit from 0 to 9. For instance, 24 is a Decimal Number constructed from the digits 2 and 4. A Binary Number is constructed with digits 0 and 1. For instance, ..

#### C program to reverse a string

This program reversed the entered string that means opposite of the previous string sequence...

#### C program to reverse an Integer number

Are you want a demonstration of The C Program that reverses an Integer number? This Program reversing the sequence of digits in an Integer. After the successful compilation of the program, a message..