# Program to find if the given matrix is upper triangular or not | faceprep

Program to find if the given matrix is upper triangular or not is discussed here. A square matrix is obtained as input from the user and it is checked if it is upper triangular or not. A square matrix is said to be an upper triangular matrix if all the entries below the diagonal are zero.

## Algorithm to check if the given matrix is upper triangular or not

• Input the order of the square matrix.
• Input the matrix elements.
• Repeat from i = 1 to n.
• Repeat from j = 0 to i.
• if (mat[i][j] != 0)
• Set flag = 0 and print “Not An Upper Triangular Matrix”.
• Else, print “Upper Triangular Matrix”.

Program to check if the given matrix is upper triangular or not is given below.

/* C program to check if the given matrix is upper triangular or not */
#include <stdio.h>

int main()
{
int n;
scanf(“%d”,&n);
int flag = 0;
int mat[n][n];
int i, j;
for(i = 0; i < n; i++)
{
for(j = 0; j < n; j++)
scanf(“%d”,&mat[i][j]);
}

for (i = 1; i < n; i++)
for (j = 0; j < i; j++)
if (mat[i][j] != 0)
flag = 0;
else
flag = 1;

if (flag == 1)
printf(“Upper Triangular Matrix”);
else
printf(“Not an Upper Triangular Matrix”);
return 0;
}

/* C++ program to check if the given matrix is upper triangular or not */
#include <iostream>
using namespace std;

int main()
{
int n;
cin >> n;
int flag = 0;
int mat[n][n];
int i, j;
for(i = 0; i < n; i++)
{
for(j = 0; j < n; j++)
cin >> mat[i][j];
}

for (i = 1; i < n; i++)
for (j = 0; j < i; j++)
if (mat[i][j] != 0)
flag = 0;
else
flag = 1;

if (flag == 1)
cout << “Upper Triangular Matrix”;
else
cout << “Not an Upper Triangular Matrix”;
return 0;
}

// Java Program to find if the given matrix is upper triangular or not

import java.util.*;
public class Main
{
public static void main(String[] args)
{
int n, flag = 0;
Scanner sc = new Scanner(System.in);
System.out.print(“\nEnter the order of the matrix : “);
n = sc.nextInt();
int[][] mat1 = new int[n][n];
System.out.print(“\nInput the matrix elements : \n”);
int i, j;
for(i = 0; i < n; i++)
{
for(j = 0; j < n; j++)
mat1[i][j] = sc.nextInt();
}
for (i = 1; i < n; i++)
for (j = 0; j < i; j++)
if (mat1[i][j] != 0)
flag = 0;
else
flag = 1;

if (flag == 1)
System.out.println(“Upper Triangular Matrix”);
else
System.out.println(“Not an Upper Triangular Matrix”);

}
}

# Python program to check if the given matrix is upper triangular or not

mat = [[1, 2, 3], [4, 0, 6], [0, 0, 9]]
flag = 0
for i in range(1, n):
for j in range(0, i):
if (mat[i][j] != 0):
flag = 0
else:
flag = 1

if (flag == 1):
print(“Upper Triangular Matrix”)
else:
print(“Not an Upper Triangular Matrix”)

#include<stdio.h>
#include<stdlib.h>
int main()
{
int **a,n,i,j,check=0;
scanf(“%d”,&n);

a=(int**)malloc(n*sizeof(int*));
for(i=0;i<n;i++)
*(a+i)=(int*)malloc(n*sizeof(int));

for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
scanf(“%d”,*(a+i)+j);
}

for(i=1;i<n;i++)
{
for(j=0;j<i;j++)
{
if(*(*(a+i)+j)!=0)
{
check++;
break;
}
}
}
if(check==0)
printf(“Upper triangular matrix”);
else
printf(“Not a upper triangular matrix”);
return 0;

}

Output: