Code Scrappers


Addition of all elements in a matrix C program


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

              void main()
              {
                  int i, j, r, c, sum = 0;

                  int m[8][8];

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

                  scanf("%d%d", &r, &c);

                  printf("\n enter the elements of the matrix :\n ");

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

                 for (i = 0; i < r; i++)
                 {
                      for (j = 0; j < c; j++)
                      {
                          sum = sum + m[i][j];
                      }
                 }

                 printf("\n addition of all elements : %d", sum);
              }
          



Output

      enter the number of rows and columns : 2
      2

       enter the elements of the matrix :
       1
       2
       3
       4

       addition of all elements : 10