The Program in C to print 3*3 matrix and print its transpose is given below:
#include <stdio.h>
int main() {
int a[3][3], i, j;
printf("Enter elements of 3x3 matrix: \n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
scanf("%d", &a[i][j]);
}
}
printf("Original Matrix: \n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf("%d ", a[i][j]);
}
printf("\n");
}
printf("Transpose of Matrix: \n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf("%d ", a[j][i]);
}
printf("\n");
}
return 0;
}
Output:
Enter elements of 3×3 matrix:
3
4
5
8
9
1
3
6
7
Original Matrix:
3 4 5
8 9 1
3 6 7
Transpose of Matrix:
3 8 3
4 9 6
5 1 7
Pro-Tips💡
This program uses two nested for loops to read the elements of the 3×3 matrix from the user.
The first for loop iterates over the rows and the second for loop iterates over the columns. After reading the matrix, the program uses two nested for loops again to print the original matrix.
Finally, the program uses two nested for loops again to print the transpose of the matrix.
The transpose of a matrix is simply obtained by swapping the rows and columns of the original matrix, which is achieved by changing the order of the indices in the array.
Learn C-Sharp ↗
C-sharp covers every topic to learn about C-Sharp thoroughly.
Learn C Programming ↗
C-Programming covers every topic to learn about C-Sharp thoroughly.
Learn C++ Programming↗
C++ covers every topic to learn about C-Sharp thoroughly.