Code Scrappers


Sum Of Daigonal elements of 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 number of rows and columns : ");

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

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

                  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++)
                      {
                          if (i == j)

                          sum = sum + m[i][j];
                      }
                 }

                 printf("\nsum of diagonal elements : %d", sum);

              }


          



Output

    enter number of rows and columns : 2
    2

    enter the elements of matrix : 1
    2
    3
    4

    sum of diagonal elements : 5