#include <stdio.h>
int main() {
// declare input and output arrays, and variables
// for the number of rows and columns. there are
// also two loop variables i and j.
int a[10][10], b[10][10], sum[10][10], rows, cols, i, j;
printf("Enter the number of rows:\n");
scanf("%d", &rows);
printf("Enter the number of columns:\n");
scanf("%d", &cols);
// check if the number of rows/columns requested
// is too small or too large.
if (rows < 1 || cols < 1 || rows > 10 || cols > 10) {
printf("Invalid number of rows or columns\n");
// exit the program
return 0;
}
// ask for the numbers
printf("Enter the numbers of the first matrix\n");
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
// array index starts from 0, but row or
// column number starts from 1.
printf("Enter row %i, column %i:\n", i + 1, j + 1);
scanf("%d", &a[i][j]);
}
}
printf("Enter the numbers of the second matrix\n");
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
// array index starts from 0, but row or
// column number starts from 1.
printf("Enter row %i, column %i:\n", i + 1, j + 1);
scanf("%d", &b[i][j]);
}
}
// calculate the sum of the matrix
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
sum[i][j] = a[i][j] + b[i][j];
}
}
printf("The first matrix is:\n");
// print the first matrix
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
printf("%d ", a[i][j]);
}
// to seperate the rows
printf("\n");
}
printf("The second matrix is:\n");
// print the second matrix
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
printf("%d ", b[i][j]);
}
// to seperate the rows
printf("\n");
}
printf("Sum of the matrix is:\n");
// print the sum
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
printf("%d ", sum[i][j]);
}
// to seperate the rows
printf("\n");
}
return 0;
}
To embed this project on your website, copy the following code and paste it into your website's HTML: