Sunday, February 25, 2018

C Program : Given an NxN matrix. You have to determine whether the matrix is a triangular matrix.

A matrix is upper triangular if every entry below the diagonal is
0. For example,  
1 1 1
0 0 1
0 0 2
is an upper triangular matrix. (The diagonal itself, and the entries
above and below the diagonals can be zeroes or non-zero integers.) 

A matrix is lower triangular if every entry above the diagonal is
0. For example, 
2 0 0
3 1 0
4 2 2
is a lower triangular matrix. 

A matrix is triangular if it is either upper triangular or lower
triangular or both.  

#include<stdio.h>

int main()
{

  int i, a,n,s=0,countl=0, countu=0;
  scanf("%d",&n);
  if(n>1)
{
  for(int r=0;r<n;r++)
  {
     for(int c=0;c<n;c++)
     {
          scanf("%d",&a);
       if((c>r)&& (a==0))
         countl++;
       if((c<r)&& (a==0))
         countu++;
     
     }
  }

  for(int j=1;j<=n;j++)
  { i=n-j;
    s=s+i;
  }
  if( (countl>=s) || (countu>=s))
  {printf("yes\n");
  
  }
  else
  {
   printf("no\n");
  
  }
}
  else
    printf("no\n");
  return 0;
}

Thanks
Happy Computing !

No comments:

Post a Comment

How can I run a C++ program directly from Windows?

How-can-I-run-a-C-program-directly-from-Windows