Code Scrappers


Addition Of two Matrices C program


              #include<stdio.h>
              #include<stdlib.h>

              void main()
              {
                    int m1[10][10], m2[10][10], m3[10][10];

                    int i, j, r1, c1, r2, c2;

                    printf("\n enter the number of rows and columns of matrix1 : ");

                    scanf("%d%d", &r1, &c1);

                    printf("\n enter the number of rows of columns matrix2 : ");

                    scanf("%d%d", &r2, &c2);

                    if (r1 != r2 || c1 != c2)
                    {
                          printf("\n Not Possible. Order of two matrices should be same. ");

                          exit(0);
                    }

                    printf("Enter the elements of matrix1 \n ");

                    for (i = 0; i < r1; i++)
                    {
                          for (j = 0; j < c1; j++)
                          {
                                scanf("%d", &m1[i][j]);
                          }
                    }

                      printf("Enter the elements of matrix2 \n ");

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

                        for (j = 0; j < c2; j++)
                        {
                              scanf("%d", &m2[i][j]);
                        }

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

                        for (j = 0; j < c1; j++)
                        {
                              m3[i][j] = m1[i][j] + m2[i][j];
                        }

                    printf("\n Addition of two Matrices : \n");

                    for (i = 0; i < r1; i++)
                    {
                          for (j = 0; j < c1; j++)
                          {
                                printf("%d\t", m3[i][j]);
                          }
                          printf("\n");
                    }

              }



          



Output

      enter the number of rows and columns of matrix1 : 2
      2

      enter the number of rows of columns matrix2 : 2
      2

      Enter the elements of matrix1
      1
      2
      3
      4

      Enter the elements of matrix2
      1
      2
      3
      4

      Addition of two Matrices :
      2       4
      6       8