C Program For Addition Of Diagonal Elements In Matrix(2D-Array)

C Program For Addition Of Diagonal Elements In Matrix(2D-Array)

#include<stdio.h>

int main()
{

 int i, j, mat[10][10], row, col;
   int sum = 0;

   printf("Enter the number of Rows : \n");
   scanf("%d", &row);  //input from user

   printf("Enter the number of Columns : \n");
   scanf("%d", &col);    //input from user

 
   for (i = 0; i < row; i++)
    {
      for (j = 0; j < col; j++)
    {
         printf("Enter the Element a[%d][%d] : \n", i, j);
         scanf("%d", &mat[i][j]);  //Array Element Entry
      }
    }

   //Addition of Diagonal Elements
   for (i = 0; i < row; i++)
    {
      for (j = 0; j < col; j++)
 {
         if (i == j)
            sum = sum + mat[i][j];
      }
   }

   printf("Sum of Diagonal Elements in Matrix : %d\n", sum);    //Print out the Result

  ]
}
Copyright © C Program | Java | OpenGL Programming | Hadoop at Computaholics