Program to print double headed arrow pattern
Given an integer N which is the number of rows, the task is to draw the number pattern in the shape of a double headed arrow.
Prerequisite: The pattern is a grow and shrink type pattern and hence basic knowledge to execute loops is required to understand the topic and the code in any language. The geometric shape can be visualized as-

Examples:
Input: R = 7
Output:
1
2 1 1 2
3 2 1 1 2 3
4 3 2 1 1 2 3 4
3 2 1 1 2 3
2 1 1 2
1
Input: R = 9
Output:
1
2 1 1 2
3 2 1 1 2 3
4 3 2 1 1 2 3 4
5 4 3 2 1 1 2 3 4 5
4 3 2 1 1 2 3 4
3 2 1 1 2 3
2 1 1 2
1
Approach:
- In the given example, N=7 and the number of ROWS is 7.
- VERTICALLY, the pattern GROWS till ROW=N/2 and SHRINKS afterwards.
- ROW 1 has 4 ” “(SPACE) characters and then a value.
- The number of SPACE characters decreases whereas NUMERALS increase in count in each successive row.
- Also, note that the 1st value of the number placed in each row is the same as the number of the row.
- Also HORIZONTALLY the pattern has NUMERALS, then SPACES and NUMERALS afterwards.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <iostream> using namespace std; // Function to print the requried pattern void drawPattern(int N) { int n = N; int row = 1; // 'nst' is the number of values int nst = 1; // 'nsp' is the number of spaces int nsp1 = n - 1; int nsp2 = -1; int val1 = row; int val2 = 1; while (row <= n) { // Here spaces are printed // 'csp' is the count of spaces int csp1 = 1; while (csp1 <= nsp1) { cout << " " << " "; csp1 = csp1 + 1; } // Now, values are printed // 'cst' is the count of stars int cst1 = 1; while (cst1 <= nst) { cout << val1 << " "; val1 = val1 - 1; cst1 = cst1 + 1; } // Again spaces have to be printed int csp2 = 1; while (csp2 <= nsp2) { cout << " " << " "; csp2 = csp2 + 1; } // Again values have to be printed if (row != 1 && row != n) { int cst2 = 1; while (cst2 <= nst) { cout << val2 << " "; val2 = val2 + 1; cst2 = cst2 + 1; } } cout << endl; // Move to the next row if (row <= n / 2) { nst = nst + 1; nsp1 = nsp1 - 2; nsp2 = nsp2 + 2; val1 = row + 1; val2 = 1; } else { nst = nst - 1; nsp1 = nsp1 + 2; nsp2 = nsp2 - 2; val1 = n - row; val2 = 1; } row = row + 1; } } // Driver code int main() { // Number of rows int N = 7; drawPattern(N); return 0; } |
Java
// Java implementation of the approach class GFG { // Function to print the requried pattern static void drawPattern(int N) { int n = N; int row = 1; // 'nst' is the number of values int nst = 1; // 'nsp' is the number of spaces int nsp1 = n - 1; int nsp2 = -1; int val1 = row; int val2 = 1; while (row <= n) { // Here spaces are printed // 'csp' is the count of spaces int csp1 = 1; while (csp1 <= nsp1) { System.out.print(" "); csp1 = csp1 + 1; } // Now, values are printed // 'cst' is the count of stars int cst1 = 1; while (cst1 <= nst) { System.out.print(val1 + " "); val1 = val1 - 1; cst1 = cst1 + 1; } // Again spaces have to be printed int csp2 = 1; while (csp2 <= nsp2) { System.out.print(" "); csp2 = csp2 + 1; } // Again values have to be printed if (row != 1 && row != n) { int cst2 = 1; while (cst2 <= nst) { System.out.print(val2 + " "); val2 = val2 + 1; cst2 = cst2 + 1; } } System.out.println(); // Move to the next row if (row <= n / 2) { nst = nst + 1; nsp1 = nsp1 - 2; nsp2 = nsp2 + 2; val1 = row + 1; val2 = 1; } else { nst = nst - 1; nsp1 = nsp1 + 2; nsp2 = nsp2 - 2; val1 = n - row; val2 = 1; } row = row + 1; } } // Driver code public static void main(String args[]) { // Number of rows int N = 7; drawPattern(N); } } |
Python3
# Python3 implementation of the approach # Function to print the requried pattern def drawPattern(N) : n = N; row = 1; # 'nst' is the number of values nst = 1; # 'nsp' is the number of spaces nsp1 = n - 1; nsp2 = -1; val1 = row; val2 = 1; while (row <= n) : # Here spaces are printed # 'csp' is the count of spaces csp1 = 1; while (csp1 <= nsp1) : print(" ",end= " "); csp1 = csp1 + 1; # Now, values are printed # 'cst' is the count of stars cst1 = 1; while (cst1 <= nst) : print(val1,end = " "); val1 = val1 - 1; cst1 = cst1 + 1; # Again spaces have to be printed csp2 = 1; while (csp2 <= nsp2) : print(" ",end = " "); csp2 = csp2 + 1; # Again values have to be printed if (row != 1 and row != n) : cst2 = 1; while (cst2 <= nst) : print(val2,end = " "); val2 = val2 + 1; cst2 = cst2 + 1; print() # Move to the next row if (row <= n // 2) : nst = nst + 1; nsp1 = nsp1 - 2; nsp2 = nsp2 + 2; val1 = row + 1; val2 = 1; else : nst = nst - 1; nsp1 = nsp1 + 2; nsp2 = nsp2 - 2; val1 = n - row; val2 = 1; row = row + 1; # Driver code if __name__ == "__main__" : # Number of rows N = 7; drawPattern(N); # This code is contributed by AnkitRai01 |
C#
// C# implementation of the approach using System; class GFG { // Function to print the requried pattern static void drawPattern(int N) { int n = N; int row = 1; // 'nst' is the number of values int nst = 1; // 'nsp' is the number of spaces int nsp1 = n - 1; int nsp2 = -1; int val1 = row; int val2 = 1; while (row <= n) { // Here spaces are printed // 'csp' is the count of spaces int csp1 = 1; while (csp1 <= nsp1) { Console.Write(" "); csp1 = csp1 + 1; } // Now, values are printed // 'cst' is the count of stars int cst1 = 1; while (cst1 <= nst) { Console.Write(val1 + " "); val1 = val1 - 1; cst1 = cst1 + 1; } // Again spaces have to be printed int csp2 = 1; while (csp2 <= nsp2) { Console.Write(" "); csp2 = csp2 + 1; } // Again values have to be printed if (row != 1 && row != n) { int cst2 = 1; while (cst2 <= nst) { Console.Write(val2 + " "); val2 = val2 + 1; cst2 = cst2 + 1; } } Console.WriteLine(); // Move to the next row if (row <= n / 2) { nst = nst + 1; nsp1 = nsp1 - 2; nsp2 = nsp2 + 2; val1 = row + 1; val2 = 1; } else { nst = nst - 1; nsp1 = nsp1 + 2; nsp2 = nsp2 - 2; val1 = n - row; val2 = 1; } row = row + 1; } } // Driver code public static void Main() { // Number of rows int N = 7; drawPattern(N); } } // This code is contributed by AnkitRai01 |
1
2 1 1 2
3 2 1 1 2 3
4 3 2 1 1 2 3 4
3 2 1 1 2 3
2 1 1 2
1
Time Complexity: O(N2)
Space Complexity: O(1)
Recommended Posts:
- Program to print the arrow pattern
- Python 3 | Program to print double sided stair-case pattern
- Program for Arrow Star Pattern
- Print symmetric double triangle pattern
- Program to print right and left arrow patterns
- Program to print the pattern 'G'
- Program to print the given Z Pattern
- Program to print the pattern "GFG"
- Program to print the pattern ‘D’
- Program to print the given H Pattern
- Program to print pattern
- Program to print Swastika Pattern
- Program to Print the Trapezium Pattern
- Program to print pyramid pattern
- Program to print a rectangle pattern
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.

