Add Two Matrix C Code - Coding Bot

Like Us On Facebook

Follow Us On Twitter

Drop Down Menu

Sunday, 25 December 2016

Add Two Matrix C Code

This program will first ask user to enter the number of rows and columns for the two matrices. Remember, they have to be the same for both of the matrices. Then the program will ask the user to enter the elements of the first matrix and then the elements of the second matrix in the fashion:

First Matrix :-
2 3
4 5
Second matrix :-
3 4
5 6
then output of the program will be :-
5 7
9 11


Here is the C Code to Add Two Matrix:


/* Double-Click To Select Code */
#include <stdio.h>
#include <conio.h>

void main()
{
   int m, n, c, d, first[10][10], second[10][10], sum[10][10];
 
   printf("Enter the number of rows and columns of matrix\n");
   scanf("%d%d", &m, &n);
   printf("Enter the elements of first matrix\n");
 
   for (c = 0; c < m; c++)
      for (d = 0; d < n; d++)
         scanf("%d", &first[c][d]);
 
   printf("Enter the elements of second matrix\n");
 
   for (c = 0; c < m; c++)
      for (d = 0 ; d < n; d++)
            scanf("%d", &second[c][d]);
 
   printf("Sum of entered matrices:-\n");
 
   for (c = 0; c < m; c++) {
      for (d = 0 ; d < n; d++) {
         sum[c][d] = first[c][d] + second[c][d];
         printf("%d\t", sum[c][d]);
      }
      printf("\n");
   }
}


Output:


Add Two Matrix C Code



Do you like this post? Please link back to this article by copying one of the codes below.

URL: HTML link code: Forum link code: