How to print diagonal elements?

CODE




How to print diagonal elements?

// Write a program to print diagonal elements of an array

int main()
{
int n,i,j,a[10][10];
printf("\n enter N:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("\n enter value at A[%d][%d]:",i,j);
scanf("%d",&a[i][j]);
}
}
printf("\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i==j)
printf(" %d",a[i][j]);
else
printf(" 0");
}
printf("\n");
}
}

Output:
enter N:3
 enter value at A[0][0]:1
 enter value at A[0][1]:2
 enter value at A[0][2]:3
 enter value at A[1][0]:4
 enter value at A[1][1]:5
 enter value at A[1][2]:6
 enter value at A[2][0]:7
 enter value at A[2][1]:8
 enter value at A[2][2]:9

 1 0 0
 0 5 0
 0 0 9

How to print diagonal elements? How to print diagonal elements? Reviewed by Unknown on December 31, 2018 Rating: 5

No comments:

If you have any doubt or query ,comment below:

Programming copyright © 2018-19. Powered by Blogger.