#include <stdio.h>
#define MAXROW 10
#define MAXCOL 10
int main()
{
int matrix[MAXROW][MAXCOL];
int i,j,r,c;
printf("Enter number of Rows :");
scanf("%d",&r);
printf("Enter number of Cols :");
scanf("%d",&c);
printf("\nEnter matrix elements :\n");
for(i=0;i< r;i++)
{
for(j=0;j< c;j++)
{
printf("Enter element [%d,%d] : ",i+1,j+1);
scanf("%d",&matrix[i][j]);
}
}
/*check condition to print diagonals, matrix must be square matrix*/
if(r==c)
{
/*print diagonals*/
for(i=0;i< c;i++)
{
for(j=0;j< r;j++)
{
if(i==j)
printf("%d\t",matrix[j][i]); /*print elements*/
else
printf("\t"); /*print space*/
}
printf("\n"); /*after each row print new line*/
}
}
else
{
printf("\nMatrix is not a Square Matrix.");
}
return 0;
}
Output
First Run:
Enter number of Rows :2
Enter number of Cols :3
Enter matrix elements :
Enter element [1,1] : 1
Enter element [1,2] : 2
Enter element [1,3] : 3
Enter element [2,1] : 4
Enter element [2,2] : 5
Enter element [2,3] : 6
Matrix is not a Square Matrix.
Second Run:
Enter number of Rows :4
Enter number of Cols :4
Enter matrix elements :
Enter element [1,1] : 1
Enter element [1,2] : 2
Enter element [1,3] : 3
Enter element [1,4] : 4
Enter element [2,1] : 5
Enter element [2,2] : 6
Enter element [2,3] : 7
Enter element [2,4] : 8
Enter element [3,1] : 9
Enter element [3,2] : 10
Enter element [3,3] : 11
Enter element [3,4] : 12
Enter element [4,1] : 13
Enter element [4,2] : 14
Enter element [4,3] : 15
Enter element [4,4] : 16
1
6
11
16
Output
need an explanation for this answer? contact us directly to get an explanation for this answer