Ticker

6/recent/ticker-posts

Program to add two matrix in C

 #include <stdio.h>

int main() {

  int r, c, a[10][10], b[10][10], sum[10][10], i, j;

  printf("\nEnter elements of 1st matrix:\n");

  for (i = 0; i < 2; ++i)

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

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

    }

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

  for (i = 0; i < 2; ++i)

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

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

    }


  // adding two matrices

  for (i = 0; i < 2; ++i)

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

      sum[i][j] = a[i][j] + b[i][j];

    }


  // printing the result

  printf("\nSum of two matrices: \n");

  for (i = 0; i < 2; ++i)

  {

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

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

      }

 printf("\n");

    }

}

Output

Enter elements of first matrix

1 1

1 1

Enter elements of 2nd matrix

1 1

1 1

Sum of two matrix

2 2

2 2


Post a Comment

0 Comments