Program in C to print diagonal elements of matrix
//Display all the diagonal elements of a given matrix #include<stdio.h> #include<conio.h> int main() { int a[5][5],r,c,i,j,sum=0; printf("Enter a number of rows and columns:-"); scanf("%d%d",&r,&c); printf("\nEnter %d*%d elements in matrix:-\n",r,c); for(i=0;i<r;i++) { for(j=0;j<c;j++) scanf("%d",&a[i][j]); } printf("The diagonal elements is"); for(i=0;i<c;i++) printf("%3d",a[i][i]); getch(); }Output:-
printf("The diagonal elements is"); for(i=0;i<c;i++) printf("%3d",a[i][i]);
It will be also write in this given style
printf("The diagonal elements is %d %d %d",a[0][0],a[1][1],a[2][2]);
Related Program of Matrix
1 Comments