C Program to Find Sum of Diagonal Elements of a Matrix

This is a program to do the sum of the main & opposite diagonal elements of a MxN Matrix

Problem Description

This C Program finds the sum of the main & opposite diagonal elements of a MxN Matrix. The program accepts an MxN matrix. Then adds main diagonal of matrix as well as the opposite diagonal of the matrix.

Problem Solution

1. Create a matrix and define its elements.
2. Declare two variables which will store sum of main and opposite diagonal.
3. Now run a single for loop and extract main diagonals elements adding to the first variable and opposite diagonal elements to the second variable.

Program/Source Code

Here is source code of the C program to finds the sum of the main & opposite diagonal elements of a MxN Matrix. The program is successfully compiled and tested using Turbo C compiler in windows environment. The program output is also shown below.

  1.     /*
  2.      * C program to find accept a matrix of order M x N and find
  3.      * the sum of the  main diagonal and off diagonal elements
  4.      */
  5.  
  6.     #include <stdio.h>
  7.     void main ()
  8.     {
  9.  
  10.         static int array[10][10];
  11.         int i, j, m, n, a = 0, sum = 0;
  12.  
  13.         printf("Enetr the order of the matix \n");
  14.         scanf("%d %d", &m, &n);
  15.  
  16.         if (m == n ) 
  17.         {
  18.  
  19.             printf("Enter the co-efficients of the matrix\n");
  20.             for (i = 0; i < m; ++i)
  21.             {
  22.                 for (j = 0; j < n; ++j)
  23.                 {
  24.                     scanf("%d", &array[i][j]);
  25.                 }
  26.             }
  27.  
  28.             printf("The given matrix is \n");
  29.             for (i = 0; i < m; ++i) 
  30.             {
  31.                 for (j = 0; j < n; ++j)
  32.                 {
  33.                     printf(" %d", array[i][j]);
  34.                 }
  35.                 printf("\n");
  36.             }
  37.  
  38.             for (i = 0; i < m; ++i) 
  39.             {
  40.                 sum = sum + array[i][i];
  41.                 a = a + array[i][m - i - 1];
  42.             }
  43.  
  44.             printf("\nThe sum of the main diagonal elements is = %d\n", sum);
  45.             printf("The sum of the off diagonal elements is   = %d\n", a);
  46.  
  47.         }
  48.  
  49.         else
  50.             printf("The given order is not square matrix\n");
  51.  
  52.     }
Program Explanation

1. Declare a matrix, taking order as input from users and define all its elements.
2. Declare two variable to store sum of each diagonal elements.
3. Run a for loop wherein the main diagonal element is given by index (i, i) where i is the iterator and opposite diagonal element is given by index(i, total_rows(m)-i-1).
4. The two variables are initialized to 0, which are summed up by diagonal elements.
5. Print the sum of diagonal elements.

advertisement
Runtime Test Cases
Enter the order of the matix
2 2
Enter the co-efficients of the matrix
40 30
38 90
The given matrix is
 40 30
 38 90
 
The sum of the main diagonal elements is = 130
The sum of the off diagonal elements is   = 68

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Books in C Programming, Data Structures and Algorithms.

Free 30-Day Java Certification Bootcamp is Live. Join Now!

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 40s–60s and exploring new directions in your career, I also offer mentoring. Learn more here.