Print Upper Hessenberg matrix of order N

Given a positive integer N, the task is to print the Upper Hessenberg matrix of order N which includes any one-digit random positive integer as its non-zero elements.
Upper Hessenberg matrix is a square matrix in which all of its elements below the sub-diagonal are zero. In mathematical term mat[i][j] = 0 for all i > j + 1.

Examples:

Input: N = 3
Output:
1 2 8
1 3 4
0 3 4



Input: N = 4
Output:
1 2 2 3
1 3 4 2
0 3 4 2
0 0 1 4

Approach: For printing an upper Hessenberg matrix with one-digit positive elements print zero for all the cells of the matrix where i > j + 1 and any single-digit random number with help of rand() function.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to print the Upper Hessenberg
// matrix of order n
void UpperHessenbergMatrix(int n)
{
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
  
            // If element is below sub-diagonal
            // then print 0
            if (i > j + 1)
                cout << '0' << " ";
  
            // Print a random digit for
            // every non-zero element
            else
                cout << rand() % 10 << " ";
        }
        cout << "\n";
    }
}
  
// Driver code
int main()
{
    int n = 4;
    UpperHessenbergMatrix(n);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
class GFG 
{
  
// Function to print the Lower Hessenberg 
// matrix of order n 
static void UpperHessenbergMatrix(int n) 
{
    for (int i = 1; i <= n; i++) 
    {
        for (int j = 1; j <= n; j++) 
        {
  
            // If element is above super-diagonal 
            // then print 0 
            if (i > j + 1
            {
                System.out.print(0 + " ");
            
              
            // Print a random digit for 
            // every non-zero element 
            else 
            {
                System.out.print((int)(Math.random() * 10) + " ");
            }
        }
        System.out.println();
    }
}
  
// Driver code 
public static void main(String[] args) 
{
    int n = 4;
    UpperHessenbergMatrix(n);
}
}
  
// This code is contributed by 29AjayKumar

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach
import random
  
# Function to prthe Upper Hessenberg
# matrix of order n
def UpperHessenbergMatrix(n):
  
    for i in range(1, n + 1):
  
        for j in range(1, n + 1):
  
            # If element is below sub-diagonal
            # then pr0
            if (i > j + 1):
                print('0', end = " ")
  
            # Pra random digit for
            # every non-zero element
            else:
                print(random.randint(1, 10), 
                                 end = " ")
        print()
  
# Driver code
n = 4;
UpperHessenbergMatrix(n)
  
# This code is contributed 
# by Mohit Kumar

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach 
using System;
  
class GFG
{
      
    // Function to print the Lower Hessenberg 
    // matrix of order n 
    static void UpperHessenbergMatrix(int n) 
    
        Random rand = new Random();
          
        for (int i = 1; i <= n; i++) 
        
            for (int j = 1; j <= n; j++)
            
      
                // If element is above super-diagonal 
                // then print 0 
                if (i > j + 1) 
                    Console.Write(0 + " "); 
      
                // Print a random digit for 
                // every non-zero element 
                else
                    Console.Write(rand.Next(1, 10) + " "); 
            
            Console.WriteLine(); 
        
    
      
    // Driver code 
    static public void Main ()
    {
        int n = 4; 
        UpperHessenbergMatrix(n); 
    }
}    
      
// This code is contributed by AnkitRai01

chevron_right


Output:

3 6 7 5 
3 5 6 2 
0 9 1 2 
0 0 7 0


My Personal Notes arrow_drop_up

Discovering ways to develop a plane for soaring career goals

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.





Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.