Code Scrappers


Transpose of a Matrix C program


              #include<stdio.h>
              #include<stdlib.h>

              int main()
              {
                    int a[8][8];

                    int s, i, j, temp;

                    printf("\n enter the size of matrix :");

                    scanf("%d", &s);

                    printf("\nEnter the elements of the matrix :\n");

                    for (i = 0; i < s; i++)
                    {
                          for (j = 0; j < s; j++)
                          {
                                scanf("%d", &a[i][j]);
                          }
                    }

                    for (i = 1; i < s; i++)
                    {
                          for (j = 0; j < i; j++)
                          {
                                temp = a[i][j];

                                a[i][j] = a[j][i];

                                a[j][i] = temp;
                          }
                    }

                    printf("\n Transpose of given matrix is :");

                    for (i = 0; i < s; i++)
                    {
                          printf("\n");

                          for (j = 0; j < s; j++)
                          {
                                printf("%d\t", a[i][j]);
                          }
                    }

                    return 0;
              }



          



Output

      enter the size of matrix :2

      Enter the elements of the matrix :
      1
      2
      3
      4

     Transpose of given matrix is :
      1       3
      2       4