Print Diagonal Elements of Matrix in C Language


Program Description:

Write a Program to Print Diagonal Elements of Matrix in C programming language, The program should accept a matrix from the user and print the main diagonal elements of the array.

📢 This Program is part of the Arrays and Matrix Programs Series

What are Diagonal Elements in Matrix:

The elements which lie on the main diagonal of the matrix are called Diagonal Elements of the matrix. The diagonal start from the Top Left Corner and goes all the way to the Bottom Right corner of the Matrix.

We have a matrix X with dimensions m x n. Where each element is xij, Then X[i][j] is a diagonal element if i == j

Let’s look at an example.

Matrix X:

The diagonal Elements of the Matrix X are:

11 55 99

Here is the graphical representation of the Diagonal elements of a Matrix.


Let’s look at a program to print diagonal elements of a Matrix in C programming language.


Print Diagonal Elements of Matrix in C Program Explanation:

The step-by-step explanation of the display diagonal elements program.

  1. Create two integer constants named ROWS and COLUMNS, Which holds the max number of rows and columns.(Change this number if you want to use large arrays)
  2. Start the Program by declaring a Matrix (2D Arrays) named X[ROWS][COLUMNS]. The row size and column size of the matrices are ROWS and COLUMNS respectively.
  3. Prompt the user to provide the desired Row size and Column Size, and update them in the variables rows and columns respectively.
  4. Check if the array size is going out of present limits ( 1-100) using the (rows >= ROWS || rows <= 0 || columns >= COLUMNS || columns <= 0) condition, And display an error message if the row/column sizeis beyond the limits.
  5. Take the user input for Matrix-X, We need to create two For Loops, One For Loop will iterate over the rows, and the Second For Loop will iterate over the columns
  6. Create the First For Loop ( Outer Loop), Start the iteration from i=0, and go till the i<rows (which is equal to for(i=0; i<rows; i++) ). At Each iteration of the Outer Loop,
    • Create the second For Loop ( Inner Loop). This loop is a Nested loop that will be inside the Outer Loop. Start the Loop from j=0 and go till the j<columns (i.e for(j=0; j<columns; j++) ). At each iteration of the Inner Loop
      • Prompt the user to provide the Matrix or 2D Array element and Read the element using the scanf function and Update the X[i][j] element.
      • Repeat the above step for all elements in the 2D array or matrix
  7. To Print the Diagonal Elements of the Matrix-X, We need to go through the array and print all elements where the element row index is equal to the column index. So create another two loops to iterate over the matrix.
  8. Create Outer For Loop to iterate over rows – for(i=0; i<rows; i++)
    • Create Inner For loop to iterate over columns – for(j=0; j<columns; j++), At Each Iteration,
      • Print matrix elements where i == j. – printf("%d ", X[i][j]);
  9. Once the above Step-9 is finished, The program should be printed all diagonal elements of the matrix-X
  10. Stop the Program.

Program to Print Diagonal Elements of Matrix in C Language:

Here is the program to print all diagonal elements of a matrix in c programming language

We’ve added a lot of comments to the code to help you understand it better.

Program Output:

Compile and Run the Program.

Example 1: Normal cases.


As you can see from the above output, The diagonal elements of the array are 11 55 99 69.

Let’s try a few more examples

The program is generating the excepted results.

Example 2: Negative Cases:

As excepted, The program generates an error message if the size goes beyond the limits.

Print Diagonal Elements of Matrix using Functions in C:

Here is the rewritten version of the above program, Where we created two functions readMatrix and displayDiagonal to perform the task of reading input from the user and displaying the main diagonal elements respectively.

We have created two functions in the above program

  1. The readMatrix() function is used to read the elements from the user and update the given matrix
    • Function Prototypevoid readMatrix(int rows, int columns, int A[rows][columns])
    • The readMatrix() function takes three formal arguments which are rows, columns, and Matrix A.
    • This function uses Two For loops to iterate over the given matrix A and updates its elements by using the scanf function – scanf(“%d”, &A[i][j]);
  2. The displayDiagonal() function is used to print the Diagonal Elements of the Matrix on the Standard Output.
    • The prototype of displayDiagonal() function is void displayDiagonal(int rows, int columns, int X[rows][columns])
    • Similar to the readMatrix() function, The displayDiagonal() function also takes three formal arguments. i.e rows, columns, and A[][] (2D Array)
    • The displayDiagonal() function uses Two For loops to iterate over the A matrix and prints the diagonal elements of the matrix. – printf("%d ", A[i][j]);

Program Output:

Compile and Run the Program.


Related Matrix Program:


Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

Leave a Reply