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) :
- i=j, represents main diagonal elements
- i>j, represents elements to the left of main diagonal elements
- 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