!
TUTORIALS EXAMPLES "
C Program to Find Transpose of a Matrix
In this example, you will learn to !nd the transpose of a matrix in C
programming.
To understand this example, you should have the knowledge of the following
C programming topics:
C Arrays
C Multidimensional Arrays
The transpose of a matrix is a new matrix that is obtained by exchanging the
rows and columns.
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.
Program to Find the Transpose of a Matrix
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
Enter rows and columns: 2
3
Enter matrix elements:
Enter element a11: 1
Enter element a12: 4
Enter element a13: 0
Enter element a21: -5
Enter element a22: 2
Enter element a23: 7
Entered matrix:
1 4 0
-5 2 7
Transpose of the matrix:
1 -5
4 2
0 7
Check out these related examples:
Multiply two Matrices by Passing Matrix to a Function
Add Two Matrices Using Multi-dimensional Arrays
Multiply Two Matrices Using Multi-dimensional Arrays
Find Largest Element in an Array
Find Largest Number Using Dynamic Memory Allocation
Access Array Elements Using Pointer
Print an Integer (Entered by the User)
Calculate Average Using Arrays
C Examples
Calculate Average Using Arrays
Find Largest Element in an Array
Calculate Standard Deviation
Add Two Matrices Using Multi-dimensional Arrays
Multiply Two Matrices Using Multi-dimensional Arrays
Find Transpose of a Matrix
Multiply two Matrices by Passing Matrix to a Function
Receive the latest tutorial to improve your programming skills
Enter Your Email Join
Get Latest Updates on Programiz
Enter Your Email
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
Terms And Conditions
App's Privacy Policy
App's Terms And Conditions
Copyright © Parewa Labs Pvt. Ltd. All rights reserved.