# print diagonal elements of matrix in c with code examples

Introduction:

Matrices are an essential data structure in programming, especially in numerical computing. A matrix is a rectangular array of numbers, arranged in rows and columns. In many programming languages, including C, matrices can be represented using multi-dimensional arrays.

In this article, we will discuss how to print the diagonal elements of a matrix in C, along with some code examples.

Printing Diagonal Elements of a Matrix:

A diagonal element of a matrix is an element that lies on the diagonal line of the matrix, i.e., a[i][j] where i=j. There are two types of diagonal elements in a matrix: the main diagonal and the secondary diagonal. The main diagonal of a matrix consists of elements a[i][j] where i=j, while the secondary diagonal consists of elements a[i][j] where i+j=n-1, where n is the number of rows or columns in the matrix.

To print the diagonal elements of a matrix in C, we can use a nested loop to iterate through the rows and columns of the matrix. We can then check if the current element is a diagonal element and print it.

Here's an example program that prints the diagonal elements of a matrix in C:

```#include <stdio.h>

int main() {
int rows, cols, i, j, matrix;

printf("Enter the number of rows: ");
scanf("%d", &rows);
printf("Enter the number of columns: ");
scanf("%d", &cols);

printf("Enter the elements of the matrix: \n");
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
scanf("%d", &matrix[i][j]);
}
}

printf("The diagonal elements are: ");
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
if (i == j) {
printf("%d ", matrix[i][j]);
}
}
}

return 0;
}
```

In this program, we first ask the user to enter the number of rows and columns for the matrix. We then use a nested loop to read the elements of the matrix from the user. Finally, we use another nested loop to iterate through the matrix and print the diagonal elements.

Output:

```Enter the number of rows: 3
Enter the number of columns: 3
Enter the elements of the matrix:
1 2 3
4 5 6
7 8 9
The diagonal elements are: 1 5 9
```

In this output, we can see that the program correctly prints the diagonal elements of the matrix.

Printing Secondary Diagonal Elements:

To print the secondary diagonal elements of a matrix, we can modify our program slightly by changing the condition in the inner loop. Instead of checking if i == j, we can check if i + j == n – 1.

Here's an example program that prints the secondary diagonal elements of a matrix in C:

```#include <stdio.h>

int main() {
int rows, cols, i, j, matrix;

printf("Enter the number of rows: ");
scanf("%d", &rows);
printf("Enter the number of columns: ");
scanf("%d", &cols);

printf("Enter the elements of the matrix: \n");
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
scanf("%d", &matrix[i][j]);
}
}

printf("Thesecondary diagonal elements are: ");
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
if (i + j == rows - 1) {
printf("%d ", matrix[i][j]);
}
}
}

return 0;
}
```

In this program, we use the same logic as before, but with a different condition to print the secondary diagonal elements.

Output:

```Enter the number of rows: 3
Enter the number of columns: 3
Enter the elements of the matrix:
1 2 3
4 5 6
7 8 9
The secondary diagonal elements are: 3 5 7
```

Conclusion:

In this article, we have discussed how to print the diagonal elements of a matrix in C, along with some code examples. Diagonal elements of a matrix can be useful in many applications, including numerical computations, image processing, and graph algorithms. We hope that this article has been helpful in understanding how to work with matrices in C, and in particular, how to extract diagonal elements.
Sure, I can provide some additional information on adjacent topics related to matrices in C.

Transposing a Matrix:

One common operation on matrices is to transpose them. Transposing a matrix means to swap its rows with its columns. The transpose of a matrix A is denoted by AT, where the (i, j)-th element of AT is the (j, i)-th element of A. For example, if A is a matrix with elements a[i][j], then the transpose of A, denoted by AT, is a matrix with elements a[j][i].

Here's an example program that transposes a matrix in C:

```#include <stdio.h>

int main() {
int rows, cols, i, j, matrix, transpose;

printf("Enter the number of rows: ");
scanf("%d", &rows);
printf("Enter the number of columns: ");
scanf("%d", &cols);

printf("Enter the elements of the matrix: \n");
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
scanf("%d", &matrix[i][j]);
}
}

for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
transpose[j][i] = matrix[i][j];
}
}

printf("The transpose of the matrix is: \n");
for (i = 0; i < cols; i++) {
for (j = 0; j < rows; j++) {
printf("%d ", transpose[i][j]);
}
printf("\n");
}

return 0;
}
```

In this program, we first read the elements of the matrix from the user. We then use nested loops to transpose the matrix and store the result in another array called transpose. Finally, we print the transpose of the matrix.

Output:

```Enter the number of rows: 2
Enter the number of columns: 3
Enter the elements of the matrix:
1 2 3
4 5 6
The transpose of the matrix is:
1 4
2 5
3 6
```

Matrix Multiplication:

Another common operation on matrices is multiplication. Matrix multiplication is not commutative, i.e., AB is not necessarily equal to BA. Matrix multiplication is defined as follows: given two matrices A and B, the product AB is a matrix C such that the (i, j)-th element of C is the dot product of the i-th row of A and the j-th column of B.

Here's an example program that multiplies two matrices in C:

```#include <stdio.h>

int main() {
int rows1, cols1, rows2, cols2, i, j, k, A, B, C;

printf("Enter the number of rows for matrix A: ");
scanf("%d", &rows1);
printf("Enter the number of columns for matrix A: ");
scanf("%d", &cols1);

printf("Enter the elements of matrix A: \n");
for (i = 0; i < rows1; i++) {
for (j = 0; j < cols1; j++) {
scanf("%d", &A[i][j]);
}
}

printf("Enter the number of rows for matrix B: ");
scanf("%d", &rows2);
printf("Enter the number of columns for matrix B: ");
scanf("%d", &cols2);

printf("Enter the elements of matrix B: \n");
for (i = 0; i < rows2; i++) {
for (j = 0; j < cols2; j++) {
scanf("%d", &B[i][j]);
}
}

if (cols1 != rows2) {
printf("The matrices cannot be multiplied.");
return 0;
}

for (i = 0; i < rows1; i++) {
for (j = 0; j < cols2; j++) {
C[i][j] = 0;
for (k = 0; k < cols1; k++) {
C[i][j] += A[i][k] * B[k][j];
}
}
}

printf("The product of the matrices is: \n");
for (i = 0; i < rows1; i++) {
for (j = 0; j < cols2; j++) {
printf("%d ", C[i][j]);
}
printf("\n");
}

return 0;
}
```

In this program, we first read the elements of the two matrices A and B from the user. We then check if the matrices can be multiplied by checking if the number of columns in A is equal to the number of rows in B. If they can be multiplied, we use nested loops to compute the product of the two matrices and store the result in a third matrix called C. Finally, we print the product of the two matrices.

Output:

```Enter the number of rows for matrix A: 2
Enter the number of columns for matrix A: 3
Enter the elements of matrix A:
1 2 3
4 5 6
Enter the number of rows for matrix B: 3
Enter the number of columns for matrix B: 2
Enter the elements of matrix B:
7 8
9 10
11 12
The product of the matrices is:
58 64
139 154
```

Conclusion:

In this article, we have discussed three common operations on matrices in C: printing diagonal elements, transposing, and multiplying. Matrices are a powerful data structure in programming and are used in many applications, including image processing, numerical computations, and graph algorithms. With the help of the code examples provided, you should now be able to perform these operations on matrices in C.

## Popular questions

Sure, here are five questions with answers related to the topic of printing diagonal elements of a matrix in C:

Q1. What is a diagonal element of a matrix?

A1. A diagonal element of a matrix is an element that lies on the diagonal line of the matrix, i.e., a[i][j] where i=j. There are two types of diagonal elements in a matrix: the main diagonal and the secondary diagonal.

Q2. How do you print the diagonal elements of a matrix in C?

A2. To print the diagonal elements of a matrix in C, you can use a nested loop to iterate through the rows and columns of the matrix. You can then check if the current element is a diagonal element and print it.

Q3. What is the difference between the main diagonal and the secondary diagonal of a matrix?

A3. The main diagonal of a matrix consists of elements a[i][j] where i=j, while the secondary diagonal consists of elements a[i][j] where i+j=n-1, where n is the number of rows or columns in the matrix.

Q4. What is matrix multiplication?

A4. Matrix multiplication is an operation that takes two matrices as input and produces a third matrix as output. It is not commutative, meaning AB is not necessarily equal to BA. Matrix multiplication is defined as follows: given two matrices A and B, the product AB is a matrix C such that the (i, j)-th element of C is the dot product of the i-th row of A and the j-th column of B.

Q5. How do you transpose a matrix in C?

A5. To transpose a matrix in C, you can use a nested loop to iterate through the rows and columns of the matrix. You can then swap the elements of the matrix by setting the (i, j)-th element of the transpose matrix to the (j, i)-th element of the original matrix.Finally, you can print the transpose matrix to obtain the result.

### Tag

Matrix-Diagonal-Printing ##### Ahmed Galal
As a senior DevOps Engineer, I possess extensive experience in cloud-native technologies. With my knowledge of the latest DevOps tools and technologies, I can assist your organization in growing and thriving. I am passionate about learning about modern technologies on a daily basis. My area of expertise includes, but is not limited to, Linux, Solaris, and Windows Servers, as well as Docker, K8s (AKS), Jenkins, Azure DevOps, AWS, Azure, Git, GitHub, Terraform, Ansible, Prometheus, Grafana, and Bash.
Posts created 607

## Master the art of formatting datetime in Python: Learn with these easy-to-follow code examples!

Begin typing your search term above and press enter to search. Press ESC to cancel.