LOWER TRIANGULAR MATRIX
Problem Description
A lower triangular matrix is a square matrix in which all the elements above the diagonal are zero.
That is, all the non-zero elements are in the lower triangle:
Write a C program to find whether a given matrix is a lower triangular matrix or not.
Input Format:
The input consists of (n*n+1) integers. The first integer corresponds to the number of rows/columns in the matrix. The remaining integers correspond to the elements in the matrix. The elements are read in rowwise order, first row first, then second row and so on. Assume that the maximum value of m and n is 5.
Output Format:
Print yes if it is a lower triangular matrix . Print no if it is not a lower triangular matrix.
CODING ARENA
#include <stdio.h>
#include<math.h>
int main()
{
int i,j,x,y,flag=0;
scanf("%d%d",&x,&y);
int a[x][y];
for(i=0;i<x;i++)
{
for(j=0;j<y;j++)
{
scanf("%d",&a[i][j]);
}
}
for(i=0;i<x;i++)
{
for(j=i+1;j<y;j++)
{
if(a[i][j]!=0)
{
flag=1;
}
}
}
if(flag==0)
printf("yes");
else
printf("no");
return 0;
}
Test Case 1
Input (stdin)3 3
1 2 3
4 3 1
8 3 4
Expected Output
no
Test Case 2
Input (stdin)3 3
1 0 0
2 8 0
4 9 9
Expected Output
yes
No comments:
Post a Comment