C Program to Find Sum of Diagonal, Non Diagonal Elements of Matrix

C Program to Find Sum of Diagonal, Non Diagonal Elements of Matrix

Write C Program to Find Sum of Diagonal, Non Diagonal Elements of Matrix

// C Program to Find Sum of Diagonal, Non Diagonal Elements of Matrix

#include <stdio.h>

int main()
{
    int i,j ,sum1 = 0,sum2 = 0;
	int A[3][3] = { {1, 2, 0},{4, 0, 6},{7, 8, 0} };

	printf("Matrix A :--> \n");
	for(i = 0;i < 3; i++)
	{
		for( j = 0; j < 3; j++)
		{
			printf("%d  ", A[i][j]);
		}
		printf("\n");
	}

	for (i = 0; i < 3; i++)
	{
		for(j = 0; j < 3; j++)
		{
			if(i == j)
			{
				sum1 += A[i][i];
			}
			else
			{
				sum2 += A[i][j];
			}

		}
	}
	printf("\n\nSum of Diagonal Elements :--> %d", sum1);
	printf("\nTSum of Non Diagonal Elements :--> %d", sum2);
	return 0;
}

Output:

Matrix A :-->
1  2  0
4  0  6
7  8  0


Sum of Diagonal Elements :--> 1
Sum of Non Diagonal Elements :--> 27