Impact-Site-Verification: dbe48ff9-4514-40fe-8cc0-70131430799e

Search This Blog

Print lower triangular matrix pattern from given array


Print lower triangular matrix pattern from given array

Input :  1 2 3 4 5
Output :
         1 0 0 0 0
         1 2 0 0 0
         1 2 3 0 0
         1 2 3 4 0
         1 2 3 4 5

Input :  1 2 3
Output :   1 0 0
                1 2 0
                1 2 3

Prerequisite:

Concept to work with Lower & Upper triangular Matrix:


Program to print lower triangular MATRIX from a square MATRIX:



MATLAB CODE:

clc
clear all
close all
x=input('Enter the array:');
l=length(x);
for i=1:l
    k=1;
    for j=1:l
        if(i<j)
            y(i,j)=0;
        else
        y(i,j)=x(k);
        k=k+1;
        end
    end
end

Explanation:


       
Sample Output:


JAVA CODE:

import java.util.Scanner;
class Lovu
{
public static void main(String args[])
{
Scanner obj=new Scanner(System.in);
System.out.println("Enter the array length:");
int l=obj.nextInt();
int ak[]=new int[l];
for(int i=0;i<l;i++)
{
ak[i]=obj.nextInt();
}
int a[][]=new int[l][l];
for(int i=0;i<l;i++)
{
int p=0;
for(int j=0;j<l;j++)
{
if(i<j)
{
a[i][j]=0;
}
else
{
a[i][j]=ak[p];
p=p+1;
}
}
}
for(int i=0;i<l;i++)
{
for(int j=0;j<l;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}
}
}
Sample Output:


No comments

Popular Posts

Followers