## Algorithm

Here's the algorithm for the provided C program that adds two matrices:

- Include the standard input/output header file.

2. Define constants.
- Define MAX_SIZE as the maximum size for matrices.

3. Declare main function.
- Declare three 2D arrays: mat1, mat2, and sum.
- Declare variables: row, col, i, and j.

4. Input Matrix Size.
- Prompt the user to enter the number of rows and columns for the matrices.

5. Input Elements for the First Matrix.
- Use nested loops to input elements for the first matrix (mat1).

6. Input Elements for the Second Matrix.
- Use nested loops to input elements for the second matrix (mat2).

- Use nested loops to add corresponding elements of mat1 and mat2 and store the result in the sum matrix.

8. Display the Result.
- Use nested loops to print the sum matrix.

9. End of Program.
- Return 0 to indicate successful program execution.

The algorithmic steps describe the flow and functionality of the provided C program without presenting the specific code.

## Code Examples

### #1 Code Example-C Programing to Add Two Matrices:

```Code - C Programming```

``````#define MAX_SIZE 100
int main()
{
int mat1[MAX_SIZE][MAX_SIZE];
int mat2[MAX_SIZE][MAX_SIZE];
int sum[MAX_SIZE][MAX_SIZE];
int row, col, i,j;
printf("Enter the number of rows (between 1 and %d): ", MAX_SIZE);
scanf("%d", &row);
printf("Enter the number of columns (between 1 and %d): ", MAX_SIZE);
scanf("%d", &col);
printf("\nEnter elements of 1st matrix:\n");
for (i = 0; i <row; ++i)
{
for (j = 0; j < col; ++j)
{
printf("Enter element mat1_%d%d: ", i, j);
scanf("%d", &mat1[i][j]);
}
}
printf("\n\nEnter elements of 2nd matrix:\n");
for (i = 0; i < row; ++i)
{
for (j = 0; j < col; ++j)
{
printf("Enter element mat2_%d%d: ", i, j);
scanf("%d", &mat2[i][j]);
}
}
for (i = 0; i < row; ++i)
{
for (j = 0; j < col; ++j)
{
sum[i][j] = mat1[i][j] + mat2[i][j];
}
}
// printing the result of the sum
printf("\nSum of two matrices: \n");
for (i = 0; i < row; ++i)
{
for (j = 0; j < col; ++j)
{
printf("%d   ", sum[i][j]);
if (j == col - 1)
{
printf("\n\n");
}
}
}
return 0;
}``````
Copy The Code &

Output

cmd
Enter the number of rows (between 1 and 100): 2
Enter the number of columns (between 1 and 100): 2
Enter elements of 1st matrix:
Enter element mat1_00: 2
Enter element mat1_01: -3
Enter element mat1_10: 4
Enter element mat1_11: 5
Enter elements of 2nd matrix:
Enter element mat2_00: 1
Enter element mat2_01: 9
Enter element mat2_10: -3
Enter element mat2_11: -5
Sum of two matrices:
3 6
1 0

### #2 arrays to store the value of the matrix and some local variables for intermediate value and mathematical operation

```Code - C Programming```

``````int mat1[MAX_SIZE][MAX_SIZE];
int mat2[MAX_SIZE][MAX_SIZE];
int sum[MAX_SIZE][MAX_SIZE];
int row, col, i,j;``````
Copy The Code &

### #3 the user to enter the numbers of rows and columns for both matrices which they want to add.

```Code - C Programming```

``````printf("Enter the number of rows (between 1 and %d): ", MAX_SIZE);
scanf("%d", &row);
printf("Enter the number of columns (between 1 and %d): ", MAX_SIZE);
scanf("%d", &col);``````
Copy The Code &

### #4 enter the element for both matrices

```Code - C Programming```

``````//enter element for first matrix
for (i = 0; i < row; ++i)
{
for (j = 0; j < col; ++j)
{
printf("Enter element mat1_%d%d: ", i, j);
scanf("%d", &mat1[i][j]);
}
}
//enter element for second matrix
for (i = 0; i < row; ++i)
{
for (j = 0; j < col; ++j)
{
printf("Enter element mat2_%d%d: ", i, j);
scanf("%d", &mat2[i][j]);
}
}``````
Copy The Code &

### #5 finally add both matrices by adding the numbers in the matching positions.

```Code - C Programming```

``````// adding two matrices
for (i = 0; i < row; ++i)
{
for (j = 0; j < col; ++j)
{
sum[i][j] = mat1[i][j] + mat2[i][j];
}
}``````
Copy The Code &