Print lower triangular matrix pattern from given array

Given an array, print the array in 2D form where upper triangle has vales 0 and lower triangle has values increasing prefix sizes (First row has prefix of size 1, second row has prefix of size 2, ..)

Examples :

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 



C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation to print
// given array in 2D form
#include <bits/stdc++.h>
using namespace std;
  
// Function to print pattern
void printPattern(int arr[], int n)
{
    // array to store the final output
    int b[n][n];
    for (int i = 0; i < n; i++)
    {
       for (int j = 0; j < n; j++)
         
       // if i > j then insert arr[j] 
       // in 2D array b[i][j]
       if (i >= j)
          b[i][j] = arr[j];
  
       else
         
          // insert zero in array b[i][j]
          b[i][j] = 0;
    }
  
    // printing pattern in 2-D array i.e,
    // printing 2D array b[i][j]
    for (int i = 0; i < n; i++)
    {
      for (int j = 0; j < n; j++)
         cout << b[i][j] << " ";
      cout << endl;
    }
}
  
// Driver code
int main()
{
    int arr[] = { 1, 2, 3, 4, 5 };
    int n = sizeof(arr) / sizeof(arr[0]);
    printPattern(arr, n);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation to print
// given array in 2D form
import java.io.*;
  
class GFG 
{
    // Function to print pattern
    static void printPattern(int arr[], int n)
    {
        // array to store the final output
        int b[][] = new int[n][n];
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
                  
                // if i > j then insert arr[j] 
                // in 2D array b[i][j]
                if (i >= j)
                    b[i][j] = arr[j];
              
                else
                      
                    // insert zero in array b[i][j]
                    b[i][j] = 0;
        }
      
        // printing pattern in 2-D array i.e,
        // printing 2D array b[i][j]
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
                System.out.print(b[i][j] +" ");
            System.out.println();
        }
    }
  
    // Driver code
    public static void main (String[] args) 
    {
        int arr[] = { 1, 2, 3, 4, 5 };
        int n = arr.length;
        printPattern(arr, n);
          
    }
}
// This code is contributed by vt_m.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation to print
# given array in 2D form
  
# Function to print pattern
def printPattern(arr, n):
  
    # array to store the final output
    b = [[0 for i in range(n)] 
            for i in range(n)]
  
    for i in range(0, n):
      
        for j in range(0, n):
          
        # if i > j then insert arr[j] 
        # in 2D array b[i][j]
            if (i >= j):
                b[i][j] = arr[j];
  
            else:
          
                # insert zero in array b[i][j]
                b[i][j] = 0;
      
    # printing pattern in 2-D array i.e,
    # printing 2D array b[i][j]
    for i in range(0, n):
      
        for j in range(0, n):
            print(b[i][j], end = " ");
        print();
      
# Driver code
arr = [ 1, 2, 3, 4, 5 ];
n = len(arr);
printPattern(arr, n);
      
# This code is contributed by
# Shivi_Aggarwal

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation to print
// given array in 2D form
using System;
  
namespace Array
{
public class GFG
{     
    // Function to print pattern         
    static void printPattern(int []arr, int n)
    {
        // array to store the final output
        int[,] b = new int[n,n];
          
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
                  
                // if i > j then insert arr[j] 
                // in 2D array b[i][j]
                if (i >= j)
                    b[i,j] = arr[j];
              
                else
                      
                    // insert zero in array b[i][j]
                    b[i,j] = 0;
        }
      
        // printing pattern in 2-D array i.e,
        // printing 2D array b[i][j]
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
                Console.Write(b[i,j] + " ");
            Console.WriteLine();
        }
    }
  
    // Driver code
    public static void Main()
    {
        int []arr = { 1, 2, 3, 4, 5 };
        int n = arr.Length;
        printPattern(arr, n);
          
    }
  
}
  
// This code is contributed by Sam007

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP implementation to print 
// lower triangular matrix pattern
  
// Function to print pattern
function printPattern(array $arr, $n)
{
    // $array to store the 
    // final output
    $b;
    for ($i = 0; $i < $n; $i++)
    {
    for ($j = 0; $j < $n; $j++)
          
    // if $i > $j then insert $arr[$j] 
    // in 2D $array $b[$i][$j]
    if ($i >= $j)
        $b[$i][$j] = $arr[$j];
  
    else
          
        // insert zero in 
        // $array $b[$i][$j]
        $b[$i][$j] = 0;
    }
  
    // printing pattern in 2-D 
    // $array i.e, printing 2D 
    // $array $b[$i][$j]
    for ($i = 0; $i < $n; $i++)
    {
    for ($j = 0; $j < $n; $j++)
        echo $b[$i][$j]." ";
    echo "\n";
    }
}
  
// Driver code
$arr = array(1, 2, 3, 4, 5);
$n = sizeof($arr) / sizeof($arr[0]);
printPattern($arr, $n);
  
// This code is contributed by Mithun Kumar
?>

chevron_right



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 


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.