In this article, we will learn how to print the pattern G using stars and white-spaces. Given a number n, we will write a program to print the pattern G over n lines or rows.
Input : 7 Output : *** * * * *** * * * * *** Input : 9 Output : ***** * * * * *** * * * * * * *****
In this program, we have used the simple logic of iteration over lines to create the pattern G. Please look at the image below which represents the pattern G in the form of a 2-d matrix, where mat[i][j] = ‘ij’:
If we try to analyze this picture with a (row, column) matrix and the circles represent the position of stars in the pattern G, we will learn the steps. Here we are performing the operations column-wise. So for the first line of stars, we set the first if condition, where the row position with 0 and (n-1) won’t get the stars and all other rows from 1 to (n-1), will get the stars. Similarly, for the second, third and fourth column we want stars at the position row = 0 and row = (n-1). The other steps are self-explanatory and can be understood from the position of rows and columns in the diagram.
Below is the implementation of above idea:
*** * * * *** * * * * ***
- Program to print pattern
- Program to print the given H Pattern
- Program to print the pattern ‘D’
- Program to print the given Z Pattern
- Program to print the pattern "GFG"
- Program to Print the Trapezium Pattern
- Program to print numeric pattern | Set - 2
- Program to print number pattern
- Program to print pyramid pattern
- Program to print V and inverted-V pattern
- Program to print Kite Pattern
- Program to print the arrow pattern
- Program to print a pattern of numbers
- Program to print Swastika Pattern
- Program to print Crown 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 email@example.com. 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.