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

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

lower triangular or not

Algorithm to check if the given matrix is lower triangular or not

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

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

/* C program to check if the given matrix is Lower triangular or not */
#include
#define N 3

int check_lower_triangular_matrix(int mat[N][N])
{
int i, j;
for (i = 0; i < N; i++)
for (j = i + 1; j < N; j++)
if (mat[i][j] != 0)
return 0;
return 1;
}

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

if (check_lower_triangular_matrix(mat))
printf(“Lower Triangular Matrix”);
else
printf(“Not a Lower Triangular Matrix”);
return 0;
}

/* C++ program to check if the given matrix is Lower triangular or not */

#include <bits/stdc++.h>
#define N 3
using namespace std;

bool check_lower_triangular_matrix(int mat[N][N])
{
for (int i = 0; i < N; i++)
for (int j = i + 1; j < N; j++)
if (mat[i][j] != 0)
return false;
return true;
}

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

if (check_lower_triangular_matrix(mat))
cout << “Lower Triangular Matrix”;
else
cout << “Not a Lower Triangular Matrix”;
return 0;
}

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

import java.util.*;
public class Main
{
static int N = 3;
public static int check_lower_triangular(int mat[][])
{
int i, j;
for (i = 0; i < N; i++)
for (j = i + 1; j < N; j++)
if (mat[i][j] != 0)
return 0;
return 1;
}
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();
}
int res = check_lower_triangular(mat1);
if (res == 1)
System.out.println(“Lower Triangular Matrix”);
else
System.out.println(“Not an Lower Triangular Matrix”);

}
}

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

def islowertriangular(M):
for i in range(0, len(M)):
for j in range(i + 1, len(M)):
if(M[i][j] != 0):
return False
return True

mat = [[1, 2, 3], [0, 5, 0], [0, 0, 0]]
if islowertriangular(mat):
print (“Lower Triangular Matrix”)
else:
print (“Not a Lower 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=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(*(*(a+i)+j)!=0)
{
check++;
break;
}

}
}
if(check!=0)
printf(“Not a lower triangular matrix”);
else
printf(“Lower triangular matrix”);
}

Output:

lower triangular matrix