Thursday, 26 November 2020

Printing diagonal elements of a given matrix in C language

Printing diagonal elements of a given matrix in language: 

In this we are going to display left and right diagonal elements of a given matrix in language.

#include <stdio.h>
void main(){
int matrix1[3][3],i, j;

 // reading the elements of first matrix
    printf(" enter elements of first matrix ");
    for( i=0; i<3 ;i++){
        for(j=0;j<3;j++){
            scanf( "%d", &matrix1[i][j]);
        }
    }

    // printing the resultant matrix  
    printf(" the matrix is  \n");
    for (i=0; i<3; i++){
        for( j=0; j<3; j++){
            printf(" %d ", matrix1[i][j]);
        }
        printf(" \n ");
    }
printf(" the left diagonal elements of this matrix are \n");

printf( "%d,   %d,   %d ",  matrix1[0][0],matrix1[1][1],matrix1[2][2]);

printf(" the right diagonal elements of this matrix are \n");

printf(" %d,   %d,   %d ",  matrix1[0][2],matrix1[1][1],matrix1[2][0]);
}

No comments:

Post a Comment