C Program to transpose a matrix
Program
#include<stdio.h>
void main()
{
int arr[10][10], size, i, j, temp;
printf("Enter the size of matrix :\t");
scanf("%d", &size);
printf("Enter the elements for matrix:\n");
for (i = 0; i < size; i++)
{
for (j = 0; j < size; j++)
{
scanf("%d", &arr[i][j]);
}
}
printf("Given square matrix is:\n");
for (i = 0; i < size; i++)
{
for (j = 0; j < size; j++)
{
printf("%d\t", arr[i][j]);
}
printf("\n");
}
for (i = 1; i < size; i++)
{
for (j = 0; j < i; j++)
{
temp = arr[i][j];
arr[i][j] = arr[j][i];
arr[j][i] = temp;
}
}
printf("Transpose matrix is :\n");
for (i = 0; i < size; i++)
{
for (j = 0; j < size; j++)
{
printf("%d\t", arr[i][j]);
}
printf("\n");
}
}
Output
Enter the size of matrix : 3
Enter the elements for matrix:
19
49
29
56
53
12
09
36
77
Given square matrix is:
19 49 29
56 53 12
9 36 77
Transpose matrix is :
19 56 9
49 53 36
29 12 77