Sunday 7 September 2014

Triangular Matrix

Level:Medium

In this assignment, you will be given an NxN matrix. You have to determine whether the matrix is a triangular matrix.

The diagonal of the matrix M of size NxN is the set of entries M(0,0), M(1,1), M(2,2), ..., M(N,N).

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. 

You may not use arrays for this program.


Input
First, you will be given N, which is the size of the matrix.

Then you will be given N rows of integers, where each row consists of N integers separated by spaces.

Output
If the input matrix is triangular, then print yes. Otherwise, print no.


program---


#include<stdio.h>

int main()
{int n,i,j,a,up=0,lp=0;
scanf("%d",&n);

for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&a);
if(i>j)
  {if(a!=0)
   up=1;
   }
   else if(i<j)
   {
   if(a!=0)
   lp=1;
   }
 }}
 if(up==0||lp==0)
 printf("yes\n");
 else
 printf("no\n");

 return 0;

 }  

1 comment: