Here, in this tutorial, we will write a program in C, C++, Python, and Java to transpose a matrix.
To write the program, you must know how to transpose a matrix, the for loop, and the programming language syntax.
Before we write the program, let us understand what is the transpose of a matrix.
What is the Transpose of a Matrix?
The transpose of a given matrix is obtained by interchanging its rows into columns and columns into rows.
For example: Consider a matrix A.
3 4 3
5 7 8
9 1 2
The transpose of the upper matrix would be:
3 5 9
4 7 1
3 8 2
C Program to Transpose a Matrix
#include<stdio.h>
int main()
{
int arr[10][10],r,c, i, j;
printf("Enter the number of rows: ");
scanf("%d", &r);
printf("Enter the number of Columns: ");
scanf("%d", &c);
printf("Enter the matrix element:\n");
for(i=0;i<c;i++)
{
for(j=0; j<r; j++)
{
scanf("%d", &arr[i][j]);
}
}
printf("The transpose of the matrix would be\n");
for(i=0;i<c;i++)
{
for(j=0;j<r;j++)
{
printf("%d \t", arr[j][i]);
}
printf("\n");
}
return 0;
}
Output:
Enter the number of rows: 3
Enter the number of Columns: 3
Enter the matrix element:
3 4 3 5 7 8 9 1 2
The transpose of the matrix would be
3 5 9
4 7 1
3 8 2
C++ Program to Transpose a Matrix
#include<iostream>
using namespace std;
int main()
{
int arr[10][10],r,c, i, j;
cout<<"Enter the number of rows: ";
cin>>r;
cout<<"Enter the number of Columns: ";
cin>>c;
cout<<"Enter the matrix elements:\n";
for(i=0;i<c;i++)
{
for(j=0; j<r; j++)
{
cin>>arr[i][j];
}
}
cout<<"The transpose of the matrix would be\n";
for(i=0;i<c;i++)
{
for(j=0;j<r;j++)
{
cout<<arr[j][i]<<" ";
}
cout<<endl;
}
return 0;
}
Output:
Enter the number of rows: 3
Enter the number of Columns: 3
Enter the matrix elements:
1 2 3 9 8 7 4 5 6
The transpose of the matrix would be
1 9 4
2 8 5
3 7 6
Python Program to Transpose a Matrix
a=[]
arr =[]
r= int(input("Enter the number of rows: "))
c= int(input("Enter the number id Columns: "))
print("Enter the matrix elements")
for i in range(r):
a =[]
for j in range(c):
a.append(int(input()))
arr.append(a)
print("The transpose of the matrix would be")
for i in range(c):
for j in range(r):
print(arr[j][i],end=" ")
print()
Output:
Enter the number of rows: 3
Enter the number id Columns: 3
Enter the matrix elements
12
23
12
34
45
65
47
23
877
The transpose of the matrix would be
12 34 47
23 45 23
12 65 877
Java Program to Transpose a Matrix
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int arr[][] = new int[50][50];
int r, c, i, j;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of rows: ");
r = sc.nextInt();
System.out.println("Enter the number of columns: ");
c = sc.nextInt();
System.out.println("Enter the matrix elements:\n");
for (i = 0; i < r; i++) {
for (j = 0; j < c; j++) {
arr[i][j] = sc.nextInt();
}
}
System.out.println("The transpose of the matrix is:\n");
for (i = 0; i < c; i++) {
for (j = 0; j < r; j++) {
System.out.print(arr[j][i] + " ");
}
System.out.println();
}
}
}
Output:
Enter the number of rows:
3
Enter the number of columns:
3
Enter the matrix elements:
3 4 5 6 1 2 3 9 8
The transpose of the matrix is:
3 6 3
4 1 9
5 2 8
Conclusion
We hope you have understood how to find the transpose of a matrix. We asked the user to enter the number of rows and columns of a matrix and its elements. We performed the transformation of the matrix generated by the user input.
Be extremely careful about for loops you use in the program. Otherwise, you may get an incorrect output. Understand the logic and implement it yourself.
If you face any difficulties, feel free to comment below.
People are also reading:
- Program to print the truth table for XY+Z
- Python Program to Find the Factors of Number
- Program to print the 1 to 10 Multiples of a Number
- Program to calculate the sum of two numbers
- Matrix in Python
- Python Program to Add Two Matrices
- Python Program to Find Numbers Divisible by Another Number
- Program to convert a given number of days into years, weeks and days
- Python Program to Display the Multiplication Table
- Program to Calculate Simple Interest
- Python Program to Illustrate Different Set Operations
Leave a Comment on this Post