Calculate sum of non-diagonal element in m*n matrix C Program

How to write a c program to calculate sum of non-diagonal element in m*n matrix in C Programming Language ?


Solution:
/*write a c program to calculate sum of non-diagonal element in m*n
matrix*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3],i,j,sum=0;
clrscr();
printf("\nenter the element of matrix :");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\nthe display of matrix is:\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("\t%d",a[i][j]);
}
printf("\n");
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(i!=j)
sum=sum+a[i][j];
}
}
printf("sum is :%d",sum);
getch();
}
/*
enter the element of matrix :1 2 3                                            
7 8 9                                                                        
4 5 6                                                                        
                                                                             
the display of matrix is:                                                    
        1       2       3                                                    
        7       8       9                                                    
        4       5       6                                                    
sum is :30                                                                    
*/


Learn More :