Given an integer N, the task is to print the given pattern.
Input: 3 Output: 1 2 4 3 5 7 6 8 9 Input: 4 Output: 1 2 4 7 3 5 8 11 6 9 12 14 10 13 15 16
- Create a matrix of size N X N which will store the pattern before printing.
- Store the elements in the upper triangle of the pattern. As observed the row index increases by 1 and column index decreases by 1 as you move down the diagonal.
- Once the upper triangle is completed then store the elements of the lower triangle in similar way as the upper triangle i.e. row index increases by 1 and column index decreases by 1 as you move down the diagonal.
Below is the implementation of the above approach:
1 2 4 3 5 7 6 8 9
- Print matrix in diagonal pattern
- Print all the sub diagonal elements of the given square matrix
- Print all the super diagonal elements of the given square matrix
- Filling diagonal to make the sum of every row, column and diagonal equal of 3x3 matrix
- Print matrix in snake pattern
- Print concentric rectangular pattern in a 2d matrix
- Print matrix in snake pattern from the last column
- Print lower triangular matrix pattern from given array
- Program to print a pattern of numbers
- Program to Print Pyramid Pattern using numbers
- Program to print numbers with diamond pattern
- Program to swap upper diagonal elements with lower diagonal elements of matrix.
- Program to check diagonal matrix and scalar matrix
- Program to convert given Matrix to a Diagonal Matrix
- Mirror of matrix across diagonal
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.