How to print cross diagonal elements?

CODE



How to print cross diagonal elements?
//Write a program to print cross diagonal elements of an array..
int main()
{
    int n,i,j,k,l,a[9][9];
    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]);
        }
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
            if(i==j)
                printf(" %d",a[i][j]);
            else if (i==n-1-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 3
0 5 0
7 0 9

How to print cross diagonal elements? How to print cross 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.