C Program Code to check whether a given matrix is Upper Triangular or Not

This is a sample C Program Code to check whether a given matrix is Upper Triangular or Not.
This is possible only in a square matrix

If you find any error or if you could suggest any changes in the code please let me know in the comment section.

Upper Triangular Matrix :

 0 1 1      0 1      0 1 1 1
 0 0 1      0 0      0 0 1 1
 0 0 0                 0 0 0 1
                          0 0 0 0



PROGRAM CODE: 

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
int *a[10];
int i,j,r,c,flag=0;
printf("Enter the row size: ");
scanf("%d",&r);
printf("Enter the coulumn size: ");
scanf("%d",&c);
for(i=0;i<r;i++)
{
a[i]=(int *)malloc(c*sizeof(int));
}
if(r==c)
{
printf("Enter the square Matrix: ");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",*(a+i)+j);
}
}
printf("The square Matrix is : \n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf( "\t%d",*(*(a+i)+j));
}
printf("\n");
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
if(i<j)
{
if(*(a[i]+j)==0)
{
flag=1;
}
}
if(i=j)
{
if(*(a[i]+j)!=0)
{
flag=1;
}
}
if(i>j)
{
if(*(a[i]+j)!=0)
{
flag=1;
}
}
}
}
if(flag==0)
{
printf("The matrix is upper triangular");
}
else
{
printf("The matrix is not upper triangular");
}
}
else
{
printf("Not a Square Matrix");
}
getch();
return 0;
}
view raw gistfile1.txt hosted with ❤ by GitHub



Comments