# Diagonal Matrix in C

## What is Matrix?

A collection of numbers organized in rows and columns to form a rectangular array is called a matrix. The numbers are referred to as matrix elements or entries. Matrices are far more common in everyday life than most people realize.

## Diagonal Matrix

The term "Diagonal Matrix" refers to any square matrix in which all of the elements are zero except for those that are present diagonally.

Or

A Diagonal Matrix is a square matrix in which all components except the primary diagonal elements are zero. Because all of the elements except the main diagonal elements are zeros, it is both upper and lower triangular.

The program for displaying diagonal elements is described step-by-step.

• Step-1: The maximum number of rows and columns should be stored in two integer constants called ROWS and COLUMNS.
• Step-2: Declare a Matrix (2D Arrays) with the name Arrays[ROWS][COLUMNS] to begin the program. The matrices have ROWS and COLUMNS as their respective row and column sizes.
• Step-3: Ask the user for their preferred Row and Column sizes, then change them in the corresponding variables for rows and columns.
• Step-4: Consider the user-provided input for Matrix-Array. We must design two for loops, the first of which iterates over the rows and the second of which iterates over the columns.
• Step-5: Make the First For Loop (Outer Loop), Start the iteration at x=0 and continue until x<rows (which is equivalent to for(x=0; x<rows; x++)). Every time the outer loop iterates.
• Step-6: Make the second For loop (inner loop). This loop, which is nested, will be contained within the outer loop. For example, for(y=0; y<columns; y++) would begin the loop at y=0 and continue until y<columns, every time the Inner Loop iterates.
• Step-7: Requesting the user to input the matrix or 2D array element Update the Array[x][y] element after reading the element using the scanf function.
• Step-8: For each element in the 2D array or matrix, repeat the previous step as necessary.
• Step-9: We must iterate through the array and print each element whose element row index equals its element column index in order to display the diagonal elements of the matrix-Array. So, add two more loops to iterate through the matrix.
• Step-10: For(x=0; x<rows; x++) creates an outer for loop that iterates across rows.Create an inner for loop with the following syntax: for(y=0; y<columns; y++), each time around,
• Step-11: Print the entries of the matrix where x == y. - printf("\t%d", array[x][y]);

where x!=y assign array[x][y]=0; and display matrix elements where printf("\t%d", array[x][y]);

• Step-12: After completing the preceding step, the program should output all diagonal members of the matrix-array.
• Step-13: When rows!=columns, the message "Matrix is not a Square Matrix" is printed.
• Step-14: End the program.

Example:

`//Using a C program, printing the diagonal matrix elements#include<stdio.h>int main(){    int array[10][10],x,y,row,column;printf("Enter number of rows : ");scanf("%d", &row);printf("\nEnter number of columns : ");        scanf("%d",&column);printf("\nEnter the values of the matrix :\n");        for (x=0;x<row;x++)        {            for (y=0;y<column;y++)            {printf("Enter the a[%d][%d] value = ",x,y);scanf("%d", &array[x][y]);            }//x loop        }//y loopprintf("\nThe elements of a matrix are : \n\n");     for (x=0;x<row;x++)         {            for (y=0;y<column;y++)            {                printf("\t%d",array[x][y]);            }            printf("\n\n");        }printf("\nThe Diagonals elements of a matrix are : \n\n");/*In order to print diagonals, the matrix must be square.*/    if(row==column)    {            /*print diagonals*/            for(x=0;x<row;x++)            {                for(y=0;y<column;y++)                {                    if(x==y)                    {printf("\t%d", array[x][y]);                    }                    else if(x!=y)                    {                        array[x][y]=0;printf("\t%d", array[x][y]);                    }                    /*printing the elements*/                    else                    {                        printf("\t");                    }                    /*printing the space*/                }                printf("\n\n");   /*after each row print new line*/            }    }    else    {printf("\nMatrix is not a Square Matrix.");    }    return 0;}`

Output:

`Enter number of rows : 3Enter number of columns : 3Enter the values of the matrix :Enter the a[0][0] value = 1Enter the a[0][1] value = 2Enter the a[0][2] value = 3Enter the a[1][0] value = 4Enter the a[1][1] value = 5Enter the a[1][2] value = 6Enter the a[2][0] value = 7Enter the a[2][1] value = 8Enter the a[2][2] value = 9The elements of a matrix are :1          2          34          5          67          8          9The Diagonals elements of a matrix are:1          0          00          5          00          0          9`