Lower Triangular Matrix

12 July 2022

Lower Triangular Matrix


If you are from 2023 batch student, Join our Telegram group for placement preparation and coming placement drive updates : https://t.me/talentbattle2023

Discription

NB :  An is a triangular matrix with all elements equal to 0 above the main diagonal.

Sample input 1:

3

1 0 0

4 5 0

7 8  9

Sample output 1:

Yes

 

Sample input 2:

3

0 1 0

0 0 0

0 0 0

Sample output 2:

No

 

C

#include <stdio.h>

 

int main()

{

              int n;

              scanf("%d",&n);

              int mat[n][n];

              for(int i = 0 ; i<n ; i++){

                             for(int j = 0 ; j<n ; j++){

                                           scanf("%d",&mat[i][j]) ;

                             }

              }

              int flag = 1;

              for(int i = 0 ; i<n ; i++){

                             for(int j = i+1 ; j<n ; j++){

                                           if(mat[i][j] != 0){

                                                          flag = 0;

                                                          break;

                                           }

                             }

                             if(flag == 0)

                             {

                                           break;

                             }

              }

              if(flag == 1)

              {

                             printf("Yes");

              }

              else

              {

                             printf("No");

              }

              return 0;

}

C++

#include <bits/stdc++.h>

using namespace std;

 

int main()

{

              int n;

              cin>>n;

              int mat[n][n];

              for(int i = 0 ; i<n ; i++){

                             for(int j = 0 ; j<n ; j++){

                                           cin>>mat[i][j];

                             }

              }

              bool flag = true;

              for(int i = 0 ; i<n ; i++){

                             for(int j = i+1 ; j<n ; j++){

                                           if(mat[i][j] != 0){

                                                          flag = false;

                                                          break;

                                           }

                             }

                             if(!flag)

                             {

                                           break;

                             }

              }

              if(flag)

              {

                             cout<<"Yes";

              }

              else

              {

                             cout<<"No";

              }

              return 0;

}

JAVA

import java.util.*;

import java.lang.*;

import java.io.*;

 

class Main

{

              public static void main (String[] args) throws java.lang.Exception

              {

                             Scanner sc=new Scanner(System.in);

                             int n = sc.nextInt();

                             int mat[][] = new int[n][n];

                             for(int i = 0 ; i<n ; i++){

                                           for(int j = 0 ; j<n ; j++){

                                                          mat[i][j] = sc.nextInt();

                                           }

                             }

                             boolean flag = true;

                             for(int i = 0 ; i<n ; i++){

                                           for(int j = i+1 ; j<n ; j++){

                                                          if(mat[i][j] != 0){

                                                                        flag = false;

                                                                        break;

                                                          }

                                           }

                                           if(!flag)

                                           {

                                                          break;

                                           }

                             }

                             if(flag)

                             {

                                           System.out.println("Yes");

                             }

                             else

                             {

                                           System.out.println("No");

                             }

              }

}

PYTHON

 

def IsLowerTri(a):

    for i in range(0, len(a)):

        for j in range(i+1, len(a)):

            if(a[i][j] != 0):

                    return False

    return True

    

a = [[1,9,0],

     [4,5,0],

     [7,8,9]];

 

if IsLowerTri(a):

    print ("Yes")

else:

    print ("No")

If you are from 2023 batch student, Join our Telegram group for placement preparation and coming placement drive updates : https://t.me/talentbattle2023
Related Articles

Ask Us Anything !