Add the diagonal elements of the matrix using C


Levels of difficulty: / perform operation:

C program to create a matrix. Add the diagonal elements of the matrix.

C Program

#include <stdio.h>
#include <conio.h>
void main()
{
	int a[3][3],trace=0,i,j;
	clrscr();
	printf("\nENTER VALUES OF THE MATRIX:\n");
	for(i=0;i<3;i++)
		for(j=0;j<3;j++)
	scanf("%d",&a[i][j]);
	printf("\nTHE VALUES OF THE MATRIX ARE:\n");
	for(i=0;i<3;i++)
        {
		for(j=0;j<3;j++)
			printf("%5d",a[i][j]);
		printf("\n");
	}
	for(i=0;i<3;i++)
		trace+=a[i][i];
printf("THE SUMMATION OF THE DIAGONAL ELEMENTS OF THE MATRIX IS %d",trace);
	getch();
}


Output