Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C
  • C Basics
  • C Data Types
  • C Operators
  • C Input and Output
  • C Control Flow
  • C Functions
  • C Arrays
  • C Strings
  • C Pointers
  • C Preprocessors
  • C File Handling
  • C Programs
  • C Cheatsheet
  • C Interview Questions
  • C MCQ
  • C++
Open In App
Next Article:
C Program to Find Determinant of a Matrix
Next article icon

Transpose of a Matrix in C

Last Updated : 31 Jul, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we will learn how to write a C program to find the transpose of a matrix. The transpose of a matrix is a new matrix formed by interchanging its rows with columns. In simple words, the transpose of A[][] is obtained by changing A[i][j] to A[j][i].

Note: The transpose of an m × n matrix will result in an n × m matrix.

Example

matrix-transpose in c

Algorithm to Find the Transpose of a Matrix

The idea is to run a nested loop and copy the elements of the original matrix A to the resultant matrix B, but with the row and column, indices swapped.

B[i][j] = A[j][i]

C Program to Find the Transpose of a Square Matrix

The below program finds the transpose of A[][] and stores the result in B[][], we can change N for different dimensions.

C




// C Program to find transpose
// of a square matrix
#include <stdio.h>
#define N 4
  
// This function stores transpose
// of A[][] in B[][]
void transpose(int A[][N], int B[][N])
{
    int i, j;
    for (i = 0; i < N; i++)
        for (j = 0; j < N; j++)
            // Assigns the transpose of element A[j][i] to
            // B[i][j]
            B[i][j] = A[j][i];
}
  
// Driver code
int main()
{
    int A[N][N] = { { 1, 1, 1, 1 },
                    { 2, 2, 2, 2 },
                    { 3, 3, 3, 3 },
                    { 4, 4, 4, 4 } };
  
    int B[N][N], i, j;
  
    transpose(A, B);
  
    printf("Result matrix is \n");
    for (i = 0; i < N; i++) {
        for (j = 0; j < N; j++)
            printf("%d ", B[i][j]);
        printf("\n");
    }
  
    return 0;
}
 
 
Output
Result matrix is   1 2 3 4   1 2 3 4   1 2 3 4   1 2 3 4   

Complexity Analysis

  • Time Complexity: O(n2)
  • Auxiliary Space: O(n 2)

C Program to find Transpose of a Rectangular Matrix

The below program finds the transpose of A[][] and stores the result in B[][].

C




// C program to find transpose
// of a rectangular matrix
#include <stdio.h>
#define M 3
#define N 4
  
// This function stores transpose
// of A[][] in B[][]
void transpose(int A[][N], int B[][M])
{
    int i, j;
    for (i = 0; i < N; i++)
        for (j = 0; j < M; j++)
            B[i][j] = A[j][i];
}
  
// Driver code
int main()
{
    int A[M][N] = { { 1, 1, 1, 1 },
                    { 2, 2, 2, 2 },
                    { 3, 3, 3, 3 } };
  
    // Note dimensions of B[][]
    int B[N][M], i, j;
  
    transpose(A, B);
  
    printf("Result matrix is \n");
    for (i = 0; i < N; i++) {
        for (j = 0; j < M; j++)
            printf("%d ", B[i][j]);
        printf("\n");
    }
  
    return 0;
}
 
 
Output
Result matrix is   1 2 3   1 2 3   1 2 3   1 2 3   

Complexity Analysis

  • Time Complexity: O(n*m)
  • Auxiliary Space: O(n*m)

Please refer complete article on Program to find the transpose of a matrix for more details!



Next Article
C Program to Find Determinant of a Matrix
author
kartik
Improve
Article Tags :
  • C Programs

Similar Reads

  • Add Matrix in C
    Matrices are the collection of numbers arranged in order of rows and columns. In this article, we will learn to write a C program for the addition of two matrices. The idea is to use two nested loops to iterate over each element of the matrices. The addition operation is performed by adding the corr
    4 min read
  • C Program to Find Determinant of a Matrix
    What is the Determinant of a Matrix? The determinant of a Matrix is a special number that is defined only for square matrices (matrices that have the same number of rows and columns). A determinant is used at many places in calculus and other matrices related to algebra, it actually represents the m
    7 min read
  • C Program To Find Normal and Trace of Matrix
    Here, we will see how to write a C program to find the normal and trace of a matrix. Below are the examples: Input: mat[][] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};Output: Normal = 16Trace = 15 Explanation: Normal = sqrt(1*1+ 2*2 + 3*3 + 4*4 + 5*5 + 6*6 + 7*7 + 8*8 + 9*9) = 16Trace = 1+5+9 = 15 Input: m
    2 min read
  • Matrix Multiplication in C
    A matrix is a collection of numbers organized in rows and columns, represented by a two-dimensional array in C. Matrices can either be square or rectangular. In this article, we will learn the multiplication of two matrices in the C programming language. ExampleInput: mat1[][] = {{1, 2}, {3, 4}} mat
    3 min read
  • C Program for Identity Matrix
    Introduction to Identity Matrix : The dictionary definition of an Identity Matrix is a square matrix in which all the elements of the principal or main diagonal are 1's and all other elements are zeros. In the below image, every matrix is an Identity Matrix. In linear algebra, this is sometimes call
    2 min read
  • C Program to Print Boundary Elements of a Matrix
    Here, we will print the boundary elements of a matrix using a C program: Input : 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 Output : 1 2 3 4 1 4 1 4 1 2 3 4Approach:Traverse the matrix from start to end.Assign an outer loop to point to the row and the inner row to traverse the elements of the row.if the elemen
    2 min read
  • C Program to Interchange Two Random Rows in a Matrix
    In this article, we will write a C program to interchange two random rows in a matrix. Below are the inputs that will be taken from the user: The number of rows & columns in the matrixThe elements in the matrixThe rows that will be interchanged Examples: Input: rows = 3, columns = 3 arr[i, j] =
    2 min read
  • How to access elements of a Square Matrix
    A square matrix is a matrix which includes elements in the form of Rows and Columns. Below is an example of a 5x5 matrix. A Matrix is accessed by: Matrix_Name[row_index][column_index] Below are the various ways to access a Square Matrix in different forms: Elements on the main diagonal Approach: row
    15+ min read
  • C Program to Compute the Sum of Diagonals of a Matrix
    Here, we will compute the sum of diagonals of a Matrix using the following 3 methods: Using Conditional statementsTaking Custom Input from the user whilst using Conditional StatementsUsing Functions We will keep the same input in all the mentioned approaches and get an output accordingly. Input:  Th
    5 min read
  • C Program to Traverse a Multi-Dimensional Array
    Write a C program to traverse a given multi-dimensional array that contains N elements. Examples Input: arr[2][3] = {{1, 2, 3}, {4, 5, 6}}Output: 1 2 3 4 5 6 Input: arr[3][2] = {{-1, -2}, {0, 3}, {5, 7}}Output: -1 -2 0 3 5 7 Different Ways to Traverse a Multi-Dimensional Array in CThe most common me
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences