C Program To Find Transpose of A Matrix
C Program To Find Transpose of A Matrix
To understand this example, you should have the knowledge of the following
C programming topics:
C Arrays
C Multidimensional Arrays
In this program, the user is asked to enter the number of rows r and
columns c . Their values should be less than 10 in this program.
Then, the user is asked to enter the elements of the matrix (of order r*c ).
The program below then computes the transpose of the matrix and prints it
on the screen.
1. #include <stdio.h>
2. int main() {
3. int a[10][10], transpose[10][10], r, c, i, j;
4. printf("Enter rows and columns: ");
5. scanf("%d %d", &r, &c);
6.
7. // Assigning elements to the matrix
8. printf("\nEnter matrix elements:\n");
9. for (i = 0; i < r; ++i)
10. for (j = 0; j < c; ++j) {
11. printf("Enter element a%d%d: ", i + 1, j + 1);
12. scanf("%d", &a[i][j]);
13. }
14.
15. // Displaying the matrix a[][]
16. printf("\nEntered matrix: \n");
17. for (i = 0; i < r; ++i)
18. for (j = 0; j < c; ++j) {
19. printf("%d ", a[i][j]);
20. if (j == c - 1)
21. printf("\n");
22. }
23.
24. // Finding the transpose of matrix a
25. for (i = 0; i < r; ++i)
26. for (j = 0; j < c; ++j) {
27. transpose[j][i] = a[i][j];
28. }
29.
30. // Displaying the transpose of matrix a
31. printf("\nTranspose of the matrix:\n");
Output
Entered matrix:
1 4 0
-5 2 7
C Examples
Subscribe
TUTORIALS
Python Tutorials
C Tutorials
Java Tutorials
Kotlin Tutorials
C++ Tutorials
Swift Tutorials
R Tutorials
DSA
EXAMPLES
Python Examples
C Examples
Java Examples
Kotlin Examples
C++ Examples
R Examples
COMPANY
About
Advertising
Contact
LEGAL
Privacy Policy