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

Get Certified in C Programming and Take Your Skills to the Next Level

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;
}

 

If you are Happy with DataFlair, do not forget to make us happy with your positive feedback on Google

courses

DataFlair Team

DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.

Leave a Reply

Your email address will not be published. Required fields are marked *