The Wayback Machine - https://web.archive.org/web/20240625191839/https://www.geeksforgeeks.org/hour-glass-pattern/
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.



Previous Article
Next Article

Similar Reads

Maximum sum of hour glass in matrix
Given a 2D matrix, the task is to find the maximum sum of an hourglass. An hour glass is made of 7 cells in following form. A B C D E F G Examples: Input : 1 1 1 0 0 0 1 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 Output : 7 Below is the hour glass with maximum sum: 1 1 1 1 1 1 1 Input : 0 3 0 0 0 0 1 0 0 0 1 1 1 0 0 0 0 2 4 4 0 0 0 2 4 Output : 11 Below i
11 min read
Program to convert time from 12 hour to 24 hour format
Given a time at 12-hour AM/PM format, convert it to military (24-hour) time. Note: Midnight is 12:00:00 AM on a 12-hour clock and 00:00:00 on a 24-hour clock. Noon is 12:00:00 PM on 12-hour clock and 12:00:00 on 24-hour clock Examples: Input : A single string containing a time in 12-hour clock format(hh:mm:ss AM or hh:mm:ss PM where 01 &lt;= hh
7 min read
Maximum sum of cocktail glass in a 2D matrix
Given a 2D matrix mat[][], the task is to find the maximum sum of a cocktail glass. A Cocktail glass is made of 6 cells in the following form: X X X X X X Examples: Input: mat[][] = { {1, 0, 4, 0, 0}, {0, 3, 0, 0, 0}, {1, 1, 1, 0, 0}, {0, 0, 0, 0, 0}, {0, 0, 0, 0, 0}} Output: 11 Below is the cocktail glass with maximum sum: 1 4 3 1 1 1 Input: mat[]
8 min read
Find if the glass will be empty or not when the rate of drinking is given
Given a cylinder glass with diameter equals D centimeters. The initial level of water in the glass equals H centimeters from the bottom. You drink the water with a speed of M milliliters per second. But if you do not drink the water from the glass, the level of water increases by N centimeters per second. The task is to find the time required to ma
5 min read
Program to find amount of water in a given glass
There are some glasses with equal capacity as 1 litre. The glasses are kept as follows: 1 2 3 4 5 6 7 8 9 10 You can put water to the only top glass. If you put more than 1-litre water to 1st glass, water overflows and fills equally in both 2nd and 3rd glasses. Glass 5 will get water from both 2nd glass and 3rd glass and so on. If you have X litre
15+ min read
Time when minute hand and hour hand coincide
Given time in hours, find the time in minutes at which the minute hand and hour hand coincide in the next one hour. Examples: Input : 3 Output : (180/11) minutesInput :7 Output : (420/11) minutes Approach : 1. Take two variables for hour "h1 and h2" and then find the angle "theta" [theta = (30 * h1)] and then divide it by "11" to find the time in m
4 min read
Time taken per hour for stoppage of Car
A car travels with an average speed of S km/h without any stoppage and with stoppage the speed of car reduces to an average of S1 km/h. The task is to find the time wasted per hour for the stoppage. Examples: Input: S = 50, S1 = 30 Output: 24 minInput: S = 30, S1 = 10 Output: 40 min Approach: Take the first example, Speed of car without any stoppag
3 min read
How to validate time in 12-hour format using Regular Expression
Given a string str, the task is to check whether the string is valid time in 12-hour format or not by using Regular Expression. The valid time in a 12-hour format must satisfy the following conditions: It should start from 1, 2, ... 9 or 10, 11, 12.It should be followed by a colon(:).It should be followed by two digits between 00 to 59.It should on
6 min read
How to validate time in 24-hour format using Regular Expression
Given a string str, the task is to check whether the string is valid time in 24-hour format or not by using Regular Expression. The valid time in the 24-hour format must satisfy the following conditions. It should start from 0-23 or 00-23.It should be followed by a ':'(colon).It should be followed by two digits from 00 to 59.It should not end with
6 min read
Distance traveled by Hour and Minute hand in given Time interval
Given two numbers H and M which denotes the length of the hour and minute hand and two time intervals(say T1 &amp; T2) in form of HH:MM, the task is to find the distance traveled by hour hand and minute hand between time T1 and T2. Examples: Input: H = 5, M = 7, T1 = "1:30", T2 = "10:50" Output: Distance travelled by minute hand: 410.50144006906635
10 min read
three90RightbarBannerImg