Skip to main content

Command Palette

Search for a command to run...

Sum of the two matrices in C Programming

Published
โ€ข1 min read
Sum of the two matrices in C Programming
R

Hi, I'm Raunak Kaushal, a BCA student at Lucknow University's main campus, passionate about becoming a software developer. ๐Ÿš€ With a knack for problem-solving and a passion for teaching online, I thrive as a team leader. ๐Ÿ“š Currently, I'm on an exciting journey of continuous learning, aiming to shape a brighter future in tech. Let's code and innovate together!

#include<stdio.h>

int main(){

int m, n, i, j;

printf("Enter the number of rows: ");

scanf("%d", &m);

printf("Enter the number of columns: ");

scanf("%d", &n);

int mat1[m][n], mat2[m][n], sum[m][n];

printf("Enter elements of first matrix:\n");

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

for(j = 0; j < n; j++) {

scanf("%d", &mat1[i][j]);

}

}

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

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

for(j = 0; j < n; j++) {

scanf("%d", &mat2[i][j]);

}

}

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

for(j = 0; j < n; j++) {

sum[i][j] = mat1[i][j] + mat2[i][j];

}

}

printf("Sum of the two matrices:\n");

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

for(j = 0; j < n; j++) {

printf("%d\t", sum[i][j]);

}

printf("\n");

}

return 0;

}

Output :

Enter the number of rows: 2

Enter the number of columns: 2

Enter elements of first matrix: 1 2 1 2

Enter elements of second matrix: 1 2 1 2

Sum of the two matrices:

2 4

2 4