Q4 Write a program to implement Addition of two matrices.

Admin

Write a program to implement Addition of two matrices. 

#include <stdio.h>
int main() {
  int r, c, a[100][100], b[100][100], sum[100][100], i, j;
  printf("Enter the number of rows (between 1 and 100): ");
  scanf("%d", &r);
  printf("Enter the number of columns (between 1 and 100): ");
  scanf("%d", &c);
  printf("\nEnter the elements of 1st matrix:\n");
  for (i = 0; i < r; ++i)
    for (j = 0; j < c; ++j) {
      printf("Enter the element a%d%d: ", i + 1, j + 1);
      scanf("%d", &a[i][j]);
    }
  printf("Enter the elements of 2nd matrix:\n");
  for (i = 0; i < r; ++i)
    for (j = 0; j < c; ++j) {
      printf("Enter element b%d%d: ", i + 1, j + 1);
      scanf("%d", &b[i][j]);
    }
  for (i = 0; i < r; ++i)
    for (j = 0; j < c; ++j) {
      sum[i][j] = a[i][j] + b[i][j];
    }
  printf("\nSum of two matrices: \n");
  for (i = 0; i < r; ++i)
    for (j = 0; j < c; ++j) {
      printf("%d   ", sum[i][j]);
      if (j == c - 1) {
        printf("\n\n");
      }
    }
  return 0;
}
Output
Enter the elements of 1st matrix:
Enter the element a11: 12
Enter the element a12: 13
Enter the element a13: 14
Enter the element a21: 15
Enter the element a22: 16
Enter the element a23: 17
Enter the element a31: 18
Enter the element a32: 19 
Enter the element a33: 20
Enter the elements of 2nd matrix:
Enter element b11: 21
Enter element b12: 22
Enter element b13: 23
Enter element b21: 24
Enter element b22: 25
Enter element b23: 26
Enter element b31: 27
Enter element b32: 28
Enter element b33: 29

Sum of two matrices:
33   35   37

39   41   43

45   47   49

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.