C program to display upper and lower diagonal of matrix

If you are using any software then below program will not give an error, but if you are using TURBO C then you have to make some changes like: void main() and some function like clrscr() or getch().



#include<stdio.h>
 main()
{
int a[10][10],i,j,m,n;
printf("Enter the limit of rows and columns\n");
scanf("%d%d",&m,&n); //m=row  n=column

//accepting matrix
printf("Enter %d by %d matrix\n",m,n);
if(m==n)
{
 for(i=0;i<m;i++)
 {
  for(j=0;j<n;j++)
  scanf("%d",&a[i][j]);
 }
}
else
printf("Please enter same number of rows and columns\n");

//printing matrix
 printf("Entered matrix is\n\n");
 for(i=0;i<m;i++)
 {
  for(j=0;j<n;j++)
  printf("%d\t",a[i][j]);
  printf("\n");
 }

printf("\nLower diagonal of given matrix is\n");
//lower diagonal of matrix
 for(i=0;i<m;i++)
 {
  printf("\n");
  for(j=0;j<n;j++)
  {
   if(i>j)
   printf("%d\t",a[i][j]);
  }
 }

printf("\n\nUpper diagonal of given matrix is\n");
//upper diagonal of matrix
 for(i=0;i<m;i++)
 {
  printf("\n");
  for(j=0;j<n;j++)
  {
   if(i<j)
    printf("%d\t",a[i][j]);
   else
   printf("\t");
  }
 }
}

OUTPUT:

Enter the limit of rows and columns
3 3
Enter 3 by 3 matrix
4 5 6
1 4 7
2 3 4
Entered matrix is

4       5       6
1       4       7
2       3       4

Lower diagonal of given matrix is


1
2       3

Upper diagonal of given matrix is

        5       6
                7

Leave a Reply

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

11 − eight =

This site uses Akismet to reduce spam. Learn how your comment data is processed.