Open In App

Hour-glass Pattern

Last Updated : 17 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given positive integer n, print numeric pattern in form of an hourglass.
Examples : 
 

Input : rows_no = 7
Output :

1 2 3 4 5 6 7
 2 3 4 5 6 7
  3 4 5 6 7
   4 5 6 7
    5 6 7
     6 7 
      7
     6 7
    5 6 7
   4 5 6 7
  3 4 5 6 7
 2 3 4 5 6 7
1 2 3 4 5 6 7

 

 

C++




// CPP code for hour glass
// pattern.
#include <iostream>
using namespace std;
  
// Function definition
void pattern(int rows_no)
{
    int i, j, k;
  
    // for loop for printing
    // upper half
    for (i = 1; i <= rows_no; i++) {
  
        // printing i spaces at
        // the beginning of each row
        for (k = 1; k < i; k++) 
            cout << " ";
          
        // printing i to rows value
        // at the end of each row
        for (j = i; j <= rows_no; j++) 
            cout << j << " ";        
  
        cout << endl;
    }
  
    // for loop for printing lower half
    for (i = rows_no - 1; i >= 1; i--) {
  
        // printing i spaces at the
        // beginning of each row
        for (k = 1; k < i; k++) 
            cout << " ";
          
  
        // printing i to rows value
        // at the end of each row
        for (j = i; j <= rows_no; j++) 
            cout << j << " ";        
  
        cout << endl;
    }
}
  
// Driver code
int main()
{
    // taking rows value from the user
    int rows_no = 7;
  
    pattern(rows_no);
    return 0;
}


Java




// Java code for hour glass
// pattern.
import java.io.*;
  
class GFG 
{
    // Function definition
    static void pattern(int rows_no)
    {
        int i, j, k;
      
        // for loop for printing
        // upper half
        for (i = 1; i <= rows_no; i++) {
      
            // printing i spaces at
            // the beginning of each row
            for (k = 1; k < i; k++) 
                System.out.print(" ");
              
            // printing i to rows value
            // at the end of each row
            for (j = i; j <= rows_no; j++) 
                System.out.print(j + " "); 
      
            System.out.println();
        }
      
        // for loop for printing lower half
        for (i = rows_no - 1; i >= 1; i--) 
        {
            // printing i spaces at the
            // beginning of each row
            for (k = 1; k < i; k++) 
                System.out.print(" ");
              
            // printing i to rows value
            // at the end of each row
            for (j = i; j <= rows_no; j++) 
                System.out.print(j + " "); 
      
            System.out.println();
        }
    }
      
    // Driver code
    public static void main (String[] args) 
    {
        // taking rows value from the user
        int rows_no = 7;
      
        pattern(rows_no);
      
    }
}
  
// This code is contributed by vt_m.


Python3




# Python3 code for hour glass pattern
  
# Function definition
def pattern(rows_no):
  
    # for loop for printing upper half
    for i in range(1, rows_no + 1): 
  
        # printing i spaces at the 
        # beginning of each row
        for k in range(1, i):
            print(" ", end = "")
          
        # printing i to rows value
        # at the end of each row
        for j in range(i, rows_no + 1): 
            print(j, end = " "
  
        print()
      
    # for loop for printing lower half
    for i in range(rows_no - 1, 0, -1): 
  
        # printing i spaces at the
        # beginning of each row
        for k in range(1, i):
            print(" ", end = "")
          
        # printing i to rows value
        # at the end of each row
        for j in range(i, rows_no + 1):
            print(j, end = " ")
  
        print()
      
# Driver code
  
# taking rows value from the user
rows_no = 7
  
pattern(rows_no) 
  
# This code is contributed 
# by ihritik


C#




// C# code for hour glass
// pattern.
using System;
class GFG 
{
    // Function definition
    static void pattern(int rows_no)
    {
        int i, j, k;
      
        // for loop for printing
        // upper half
        for (i = 1; i <= rows_no; i++) {
      
            // printing i spaces at
            // the beginning of each row
            for (k = 1; k < i; k++) 
                Console.Write(" ");
              
            // printing i to rows value
            // at the end of each row
            for (j = i; j <= rows_no; j++) 
                Console.Write(j + " "); 
      
            Console.WriteLine();
        }
      
        // for loop for printing lower half
        for (i = rows_no - 1; i >= 1; i--) 
        {
            // printing i spaces at the
            // beginning of each row
            for (k = 1; k < i; k++) 
                Console.Write(" ");
              
            // printing i to rows value
            // at the end of each row
            for (j = i; j <= rows_no; j++) 
                Console.Write(j + " "); 
      
            Console.WriteLine();
        }
    }
      
    // Driver code
    public static void Main () 
    {
        // taking rows value from the user
        int rows_no = 7;
      
        pattern(rows_no);
      
    }
}
  
// This code is contributed by vt_m.


PHP




<?php
// PHP code for hour glass pattern
// Function definition
function pattern($rows_no)
{
    // for loop for printing
    // upper half
    for ($i = 1; $i <= $rows_no; $i++) 
    {
  
        // printing i spaces at
        // the beginning of each row
        for ($k = 1; $k < $i; $k++) 
            echo " ";
          
        // printing i to rows value
        // at the end of each row
        for ($j = $i; $j <= $rows_no; $j++) 
            echo $j." ";     
  
        echo "\n";
    }
  
    // for loop for printing lower half
    for ($i = $rows_no - 1; $i >= 1; $i--) 
    {
  
        // printing i spaces at the
        // beginning of each row
        for ($k = 1; $k < $i; $k++) 
            echo " ";
          
        // printing i to rows value
        // at the end of each row
        for ($j = $i; $j <= $rows_no; $j++) 
            echo $j." ";     
  
        echo "\n";
    }
}
  
// Driver code
$rows_no = 7;
pattern($rows_no);
  
// This code is contributed by mits 
?>


Javascript




<script>
// Javascript code for hour glass
// pattern.
  
    // Function definition
    function pattern( rows_no)
    {
        let i, j, k;
  
        // for loop for printing
        // upper half
        for (i = 1; i <= rows_no; i++)
        {
  
            // printing i spaces at
            // the beginning of each row
            for (k = 1; k < i; k++)
                document.write(" ");
  
            // printing i to rows value
            // at the end of each row
            for (j = i; j <= rows_no; j++)
                document.write(j + " ");
  
            document.write("<br/>");
        }
  
        // for loop for printing lower half
        for (i = rows_no - 1; i >= 1; i--)
        {
          
            // printing i spaces at the
            // beginning of each row
            for (k = 1; k < i; k++)
                document.write(" ");
  
            // printing i to rows value
            // at the end of each row
            for (j = i; j <= rows_no; j++)
                document.write(j + " ");
  
            document.write("<br/>");
        }
    }
  
    // Driver code
       
    // taking rows value from the user
    let rows_no = 7;
    pattern(rows_no);
  
// This code is contributed by gauravrajput1 
</script>


Output : 
 

1 2 3 4 5 6 7
 2 3 4 5 6 7
  3 4 5 6 7
   4 5 6 7
    5 6 7
     6 7 
      7
     6 7
    5 6 7
   4 5 6 7
  3 4 5 6 7
 2 3 4 5 6 7
1 2 3 4 5 6 7

Time Complexity : O(rows_no*rows_no) ,where rows_no is rows value taken from user.

Space Complexity : O(1) ,as we are not using any extra space.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads