Given a positive integer n. The problem is to print the pyramid pattern as described in the examples below.
Input : n = 4 Output : 1 3*2 4*5*6 10*9*8*7 Input : n = 5 Output : 1 3*2 4*5*6 10*9*8*7 11*12*13*14*15
Approach: For odd number row, values are being displayed in increasing order and for even number row, values are being displayed in decreasing order. The only other trick is to how to iterate the loops.
printPattern(int n) Declare j, k Initialize k = 0 for i = 1 to n if i%2 != 0 for j = k+1, j < k+i, j++ print j and "*" print j and new line k = ++j else k = k+i-1 for j = k, j > k-i+1, j-- print j and "*"; print j and new line
1 3*2 4*5*6 10*9*8*7 11*12*13*14*15
Time Complexity: O((n * (n + 1)) / 2)
This article is contributed by Ayush Jauhari. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Program to print pyramid pattern
- Program to print hollow pyramid and diamond pattern
- C Program to print pyramid pattern
- Program to print a inverse pyramid character pattern
- Print the pyramid pattern with given height and minimum number of stars
- Program to Print Pyramid Pattern using numbers
- Get last node of the binary tree following given pattern starting from X
- Program to print Even Odd Number Pyramid
- Programs for printing pyramid patterns in Python
- Programs for printing pyramid patterns in C++
- Programs for printing pyramid patterns in Java
- Program for volume of Pyramid
- PHP programs for printing pyramid patterns
- Programs for printing pyramid patterns using recursion
- Print a pattern without using any loop
- Print matrix in diagonal pattern
- Print pattern using only one loop | Set 1 (Using setw)
- Program to print a pattern of numbers
- Python Program to print digit pattern
- Print a String in wave pattern