# C Program Calculate Sum of Diagonal Elements of a Matrix

by Dinesh Thakur Category: C Programming (Pratical)

In this program, two variables of array type element declared. Which contains the value of declared variables and Loop statement will also use to be execute the situation. The loop statement will elaborate its functioning until the condition get false. The other function is to be comparing if (m! = n) that will end the functioning of add the Matrix given components must be define all the conditions true. And the result as output also displayed on the screen as well.

Problem Statement:
In this calculation the two diagonal elements will be added for perception of output. Write a program to read the data and determine the following:

1. Declaring required Variables.
2. Entering the Order of Matrix element.
3. Defy The Loop statement with required condition.
4. Displaying the Output on the Screen.

The Source Code is written below. This Program is well Compiled and runs well as required.

```#include<stdio.h>  void main()  {   int a,sum=0;   int i,j,m,n;   clrscr();   printf("Enter Order of Matrix = ");   scanf("%d%d",&m,&n);   if(m!=n)     {          printf("Not a Square Matrix : ");          getch();          exit();     }          printf("Enter Elements : ");          for(i=0;i<m;i++)              {                  for(j=0;j<n;j++)                      {                         scanf("%d",&a[i][j]);                      }               }                  for(i=0;i<m;i++)                      {                          for(j=0;j<n;j++)                              {                                    if(i==j)                                      {                                          sum+=a[i][j];                                       }                               }                       }                   printf("Sum of Diagonal Elements = %d ",sum);                   getch();  }
``` Related Articles on C Programming Dinesh Thakur holds an B.C.A, MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.