Sunday 8 October 2017

Convert a square matrix to Lower and Upper Triangular Matrix


Upper and Lower triangle Matrix Using C program

In this program we will be discussing on how to convert a square matrix into corresponding upper and lower triangle matrix.

LOGIC:

If for a(ij) :

  1. i=j, represents main diagonal elements
  2. i>j, represents elements to the left of main diagonal elements
  3. i<j, represents elements to the right of main diagonal elements


   #include<stdio.h>
   #include<conio.h>
   void main()
   {
   clrscr();
   int m,n,i,j;
   float a[100][100],cpy[100][100];
   printf("Enter number of rows ");
   scanf("%d",&m);
   printf("\n Enter elements for matrix \n");
   for(i=0;i<m;i++)
   for(j=0;j<m;j++)
   {
   scanf("%f",&a[i][j]);
   cpy[i][j]=a[i][j];
   }
   for(i=0;i<m;i++)
   for(j=0;j<m;j++)
   {
   if(i>j)
   a[i][j]=0;
   if(i<j)
   cpy[i][j]=0;
   }
   printf("Upper triangle matrix is \n");
   for(i=0;i<m;i++)
   {
   for(j=0;j<m;j++)
   printf("%f\t",a[i][j]);
   printf("\n");
   }
   printf("Lower triangle matrix is \n");
   for(i=0;i<m;i++)
   {
   for(j=0;j<m;j++)
   printf("%f\t",cpy[i][j]);
   printf("\n");
   }
   getch();
   }

No comments:

Post a Comment

Convert a square matrix to Lower and Upper Triangular Matrix

Upper and Lower triangle Matrix Using C program In this program we will be discussing on how to convert a square matrix into correspon...