Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR TYPESCRIPT

print diagonal elements of matrix in c

#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("
Enter matrix elements :
");
    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	",matrix[j][i]);    /*print elements*/
                    else
                        printf("	");   /*print space*/
                }
                printf("
");   /*after each row print new line*/      
            }
    }
    else
    {
        printf("
Matrix is not a Square Matrix.");
    }
    return 0;   
}
Source by www.includehelp.com #
 
PREVIOUS NEXT
Tagged: #print #diagonal #elements #matrix
ADD COMMENT
Topic
Name
9+1 =