0% found this document useful (0 votes)
8 views

number 1 code

Uploaded by

Jane Mandolado
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

number 1 code

Uploaded by

Jane Mandolado
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

#include <stdio.

h>
#include <stdlib.h>

int main() {
int matrices, rows1, cols1, rows2, cols2, i, j, k;

printf("Input Number of Matrix: ");


scanf("%d", &matrices);

if (matrices != 2) {
printf("Error: Only 2 matrices are supported.\n");
return 1;
}

printf("Size of Matrix 1 (rows & columns): ");


scanf("%d %d", &rows1, &cols1);

printf("Size of Matrix 2 (rows & columns): ");


scanf("%d %d", &rows2, &cols2);

if (rows1 != rows2 || cols1 != cols2) {


printf("Error: Sizes of matrices do not match.\n");
return 1;
}

int matrix1[rows1][cols1], matrix2[rows2][cols2], sum[rows1][cols1];

printf("Elements of matrix 1:\n");


for (i = 0; i < rows1; i++) {
for (j = 0; j < cols1; j++) {
scanf("%d", &matrix1[i][j]);
}
}

printf("Elements of matrix 2:\n");


for (i = 0; i < rows2; i++) {
for (j = 0; j < cols2; j++) {
scanf("%d", &matrix2[i][j]);
}
}

for (i = 0; i < rows1; i++) {


for (j = 0; j < cols1; j++) {
sum[i][j] = matrix1[i][j] + matrix2[i][j];
}
}

printf("Sum of matrices:\n");
for (i = 0; i < rows1; i++) {
for (j = 0; j < cols1; j++) {
printf("%d ", sum[i][j]);
}
printf("\n");
}

return 0;
}

You might also like