Search This Blog


Saturday, 20 April 2019

Program to Add two matrix in Language C

WELCOME BACK CODERS, HERE IS OUR NEW BLOG ON ADDITION OF 2 MATRIX .

IN THIS PROGRAM THE USER NEEDS TO ENTER 2 MATRIX THEN THE PROGRAM WILL ADD BOTH THE MATRIX AND DISPLAY THE RESULT.


Matrix addition in C



Matrix addition in C: C program to add two matrices, i.e., compute the sum of two matrices and then print it. Firstly a user will be asked to enter the order of matrix (number of rows and columns) and then two matrices. For example, if a user input order as 2, 2, i.e., two rows and two columns and matrices as
First matrix:
1 2
3 4

Second matrix:
4 5
-1 5

then the output of the program (Summation of the two matrices) is:
5 7
2 9

CODE
#include<stdio.h>
  
 int 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 (= 0; c < m; c++)
               for (= 0; d < n; d++)
                  scanf("%d", &first[c][d]);
          
            printf("Enter the elements of second matrix\n");
          
            for (= 0; c < m; c++)
               for (= 0 ; d < n; d++)
                  scanf("%d", &second[c][d]);
            
            printf("Sum of entered matrices:-\n");
            
            for (= 0; c < m; c++) {
               for (= 0 ; d < n; d++) {
                  sum[c][d] = first[c][d] + second[c][d];
                  printf("%d\t", sum[c][d]);
               }
               printf("\n");
            }
          
            return 0;
         }


OUTPUT


No comments:

Post a Comment