# Upper Triangular Matrix

12 July 2022

upper-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 upper triangular matrix is a triangular matrix with all elements equal to 0 below the main diagonal.

Sample input 1:

3

1 2 3

0 5 6

0 0 9

Sample output 1:

Yes

Sample input 2:

3

1 1 1

1 0 0

0 0 0

Sample output 2:

No

C Program

#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 = 0 ; j<i ; j++){

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

flag = 0;

break;

}

}

if(flag == 0)

{

break;

}

}

if(flag == 1)

{

printf("Yes");

}

else

{

printf("No");

}

return 0;

}

C++ Program

#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 = 0 ; j<i ; j++){

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

flag = false;

break;

}

}

if(!flag)

{

break;

}

}

if(flag)

{

cout<<"Yes";

}

else

{

cout<<"No";

}

return 0;

}

JAVA Program

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 = 0 ; j<i ; j++){

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

flag = false;

break;

}

}

if(!flag)

{

break;

}

}

if(flag)

{

System.out.println("Yes");

}

else

{

System.out.println("No");

}

}

}

PYTHON Program

def IsUpperTri(a):

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

for j in range(0, i):

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

return False

return True

a = [[1,2,3],

[0,5,6],

[0,0,9]];

if IsUpperTri(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