Upper,lower triangular matrix in C

 1. Program to display m*n order matrix.


#include<conio.h>
#include<stdio.h>
void main()
{
int m,n,i,j;
int a[100][20];
printf("\n  Enter the size of rows and columns\n");
scanf("%d %d",&m,&n);
printf("\n Enter the elements for matrix\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\n The elements of matrix in matrix form is i.e in m*n order form\n"); // m=size of row and n= size of column
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
   getch();
   }

Output:   


                                        
                                                                                                                                                                        2 Program for transpose of matrix


#include<conio.h>
#include<stdio.h>
void main()
{
int m,n,i,j;
int a[10][20];
printf("Enter the size of rows and columns\n");
scanf("%d %d",&m,&n);
printf("Enter the elements for matrix\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("The elements of matrix in matrix form is i.e in m*n order form\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf("The transpose of matrix is\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
printf("%d\t",a[j][i]);
}
printf("\n");
}
   getch();
   }

Output:                                                                                                                                 

















3. Program for sum of all element

#include<conio.h>
#include<stdio.h>
void main()
{
int m,n,i,j,sum=0;
int a[100][100];

printf("Enter the size of rows and columns\n");
scanf("%d %d",&m,&n);
printf("Enter the elements for matrix\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("The elements of matrix in matrix form is\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
sum=0;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
sum+=a[i][j];
}
printf("sum of all element is %d",sum);
}
   getch();
    }

Output:














4. Program for sum of diagonal of matrix

#include<conio.h>
#include<stdio.h>
void main()
{
int m,n,i,j,sum=0;
int a[100][100];
printf("Enter the size of rows and columns\n");
scanf("%d %d",&m,&n);
printf("Enter the elements for matrix\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("The elements of matrix in matrix form is\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(i==j)
{
sum+=a[i][j];
}
}
}
printf("sum of  diagonal element  of matrix is %d",sum);
printf("\n");
   getch();
    }

Output:





























5. Program to print lower triangular matrix.

#include<conio.h>
#include<stdio.h>
void main()

{

int m,n,i,j;

int a[100][100];
printf("Enter the size of rows and columns\n");
scanf("%d %d",&m,&n);
printf("Enter the elements for matrix\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("The elements of matrix in matrix form is\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
if(m==n)
printf("The lower triangular matrix is \n");

for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(i<j)
{
printf("0\t");
}
else
  {
  printf("%d\t",a[i][j]);
  }
  }
  printf("\n");
  }
getch();
  }
Output:


























6. Program to print upper triangular matrix.

#include<conio.h>

#include<stdio.h>

void main()

{

int m,n,i,j;

int a[100][100];
printf("Enter the size of rows and columns\n");
scanf("%d %d",&m,&n);
printf("Enter the elements for matrix\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("The elements of matrix in matrix form is\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
if(m==n)
printf("The uppper triangular matrix is \n");

for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(i>j)
{
printf("0\t");
}
else
  {
  printf("%d\t",a[i][j]);
  }
  }
  printf("\n");
  }

  getch();
  }
Output:



Comments