Site icon DataFlair

C Program to Print Diagonal, Transpose, Lower and Upper Triangular Matrix

Program 1

// Program for Digonal elements
#include<stdio.h>
#include<conio.h>
int main()
{
    system("cls");
    int a[50][50],m,n,r,c;
    printf("\nEnter the value of row and column");
    scanf("%d %d",&m,&n);
     if(m!=n)
        printf("\nInvalid matrix for digonal elements");
    else
   {     
      printf("\n Enter elements in matrix");
       for(r=0;r<m;r++)
       {
          for(c=0;c<n;c++)
          scanf("%d",&a[r][c]);
       }
     printf("\nElements of matrix\n");
      for(r=0;r<m;r++)
     {
        for(c=0;c<n;c++)
       { 
         printf("%4d",a[r][c]);
       }
       printf("\n");
    }
      printf("\nDigonal Elements of matrix\n");
      for(r=0;r<m;r++)
    {
        for(c=0;c<n;c++)
       { 
         if(r==c)
         printf("%4d",a[r][c]);
       }
       printf("\n");
    }
}
   return 0;
}

Program 2

// Program for lower and upper triangle
#include<stdio.h>
#include<conio.h>
int main()
{
    system("cls");
    int a[50][50],m,n,r,c;
    printf("\nEnter the value of row and column");
    scanf("%d %d",&m,&n);
     if(m!=n)
        printf("\nInvalid matrix for digonal elements");
    else
   {     
      printf("\n Enter elements in matrix");
       for(r=0;r<m;r++)
       {
          for(c=0;c<n;c++)
          scanf("%d",&a[r][c]);
       }
     printf("\nElements of matrix\n");
      for(r=0;r<m;r++)
     {
        for(c=0;c<n;c++)
       { 
         printf("%4d",a[r][c]);
       }
       printf("\n");
    }
      printf("\n Lower Triangle of  matrix\n");
      for(r=0;r<m;r++)
    {
        for(c=0;c<n;c++)
       { 
         if(r>=c)
         printf("%4d",a[r][c]);
       }
       printf("\n");
    }

      printf("\n Upper Triangle of  matrix\n");
      for(r=0;r<m;r++)
    {
        for(c=0;c<n;c++)
       { 
         if(r<=c)
         printf("%4d",a[r][c]);
       }
       printf("\n");
    }

}
   return 0;
}

Program 3

// Program for Transpose of a matrix
#include<stdio.h>
#include<conio.h>
int main()
{
    system("cls");
    int a[50][50],m,n,r,c;
    printf("\nEnter the value of row and column");
    scanf("%d %d",&m,&n);
    printf("\n Enter elements in matrix");
       for(r=0;r<m;r++)
       {
          for(c=0;c<n;c++)
          scanf("%d",&a[r][c]);
       }
     printf("\nElements of matrix\n");
      for(r=0;r<m;r++)
     {
        for(c=0;c<n;c++)
       { 
         printf("%4d",a[r][c]);
       }
       printf("\n");
    }
     printf("\nTranspose Elements of matrix\n");
      for(r=0;r<m;r++)
     {
        for(c=0;c<n;c++)
       { 
         printf("%4d",a[c][r]);
       }
       printf("\n");
    }
return 0;
}

 

Exit mobile version