Using - C
#include<stdio.h>
void main() {
int rows, cols, i,j;
printf("Enter number of Rows & Columns of Matrix: ");
scanf("%d %d", &rows, &cols);
int matrix[rows][cols];
printf("\nEnter element of %d X %d Matrix: \n", rows, cols);
for(i=0; i < rows; i++) {
for(j=0; j < cols; j++) {
scanf("%d",&matrix[i][j]);
}
}
printf("\n\nGIVEN MATRIX:\n");
for(i=0; i < rows; i++) {
for(j=0; j < cols; j++) {
printf("%d ",matrix[i][j]);
}
printf("\n");
}
printf("\nTRANSPOSE OF A MATRIX:\n");
for(i=0; i < rows; i++) {
for(j=0; j < cols; j++) {
printf("%d ",matrix[j][i]);
}
printf("\n");
}
}
OUTPUT
Enter number of Rows & Columns of Matrix: 2 2
Enter element of 2 X 2 Matrix:
23 52
56 85
GIVEN MATRIX:
23 52
56 85
TRANSPOSE OF A MATRIX:
23 56
52 85
Using - C++
#include<iostream>
using namespace std;
int main() {
int rows, cols;
cout<< "Enter number of Rows & Columns of Matrix: ";
cin>> rows >> cols;
int matrix[rows][cols];
cout<< "\nEnter elements of "<< rows << " X " << cols<< " Matrix:"<< endl;
for(int i=0; i < rows; i++) {
for(int j=0; j < cols; j++) {
cin>> matrix[i][j];
}
}
cout<< "\n\nGIVEN MATRIX:\n";
for(int i=0; i < rows; i++) {
for(int j=0; j < cols; j++) {
cout<< matrix[i][j] << " ";
}
cout<< endl;
}
cout<< "\nTRANSPOSE OF A MATRIX:\n";
for(int i=0; i < rows; i++) {
for(int j=0; j < cols; j++) {
cout<< matrix[j][i]<< " ";
}
cout<< endl;
}
return 0;
}
OUTPUT
Enter number of Rows & Columns of Matrix: 3 3
Enter elements of 3 X 3 Matrix:
2 3 5
4 8 9
1 5 4
GIVEN MATRIX:
2 3 5
4 8 9
1 5 4
TRANSPOSE OF A MATRIX:
2 4 1
3 8 5
5 9 4