C PROGRAMMING

write a c program to print diagonals of a matrix

C program to print diagonal elements of a matrix?

   #include<stdio.h>

int main()
{
int array1[10][10],i,j,m,n,sum = 0;
printf("Enter no. of rows :: ");
scanf("%d", &m);
printf("nEnter no. of cols :: ");
scanf("%d",&n);
printf("nEnter values to the matrix :: n");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
printf("nEnter a[%d][%d] value :: ",i,j);
scanf("%d", &array1[i][j]);
}
}

printf("nThe Diagonals elements of a matrix are :: nn");

/*check condition to print diagonals, matrix must be square matrix*/
if(m==n)
{
/*print diagonals*/
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{

if(i==j)
printf("t%d", array1[i][j]); /*print elements*/
else
printf("t"); /*print space*/
}
printf("nn"); /*after each row print new line*/
}
}
else
{
printf("nMatrix is not a Square Matrix.");
}
return 0;
}
Show More

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button