Inner reducing pattern printing Last Updated : 02 Mar, 2023 Comments Improve Suggest changes Like Article Like Report Try it on GfG Practice Given a number N, print the following pattern. Examples : Input : 4 Output : 4444444 4333334 4322234 4321234 4322234 4333334 4444444 Explanation: (1) Given value of n forms the outer-most rectangular box layer. (2) Value of n reduces by 1 and forms an inner rectangular box layer. (3) The step (2) is repeated until n reduces to 1. Input : 3 Output : 33333 32223 32123 32223 33333 C++ // C++ Program to print rectangular // inner reducing pattern #include <bits/stdc++.h> using namespace std; #define max 100 // function to Print pattern void print(int a[][max], int size) { for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { cout << a[i][j]; } cout << endl; } } // function to compute pattern void innerPattern(int n) { // Pattern Size int size = 2 * n - 1; int front = 0; int back = size - 1; int a[max][max]; while (n != 0) { for (int i = front; i <= back; i++) { for (int j = front; j <= back; j++) { if (i == front || i == back || j == front || j == back) a[i][j] = n; } } ++front; --back; --n; } print(a, size); } // Driver code int main() { // Input int n = 4; // function calling innerPattern(n); return 0; } // This code is contributed by Anant Agarwal. Java // Java Program to print rectangular // inner reducing pattern public class Pattern { // function to compute pattern public static void innerPattern(int n) { // Pattern Size int size = 2 * n - 1; int front = 0; int back = size - 1; int a[][] = new int[size][size]; while (n != 0) { for (int i = front; i <= back; i++) { for (int j = front; j <= back; j++) { if (i == front || i == back || j == front || j == back) a[i][j] = n; } } ++front; --back; --n; } print(a, size); } // function to Print pattern public static void print(int a[][], int size) { for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { System.out.print(a[i][j]); } System.out.println(); } } // Main Method public static void main(String[] args) { int n = 4; // Input innerPattern(n); } } Python3 # Python3 Program to print rectangular # inner reducing pattern MAX = 100 # function to Print pattern def prints(a, size): for i in range(size): for j in range(size): print(a[i][j], end = '') print() # function to compute pattern def innerPattern(n): # Pattern Size size = 2 * n - 1 front = 0 back = size - 1 a = [[0 for i in range(MAX)] for i in range(MAX)] while (n != 0): for i in range(front, back + 1): for j in range(front, back + 1): if (i == front or i == back or j == front or j == back): a[i][j] = n front += 1 back -= 1 n -= 1 prints(a, size); # Driver code # Input n = 4 # function calling innerPattern(n) # This code is contributed # by sahishelangia C# using System; public class Pattern { // function to compute pattern public static void InnerPattern(int n) { // Pattern Size int size = 2 * n - 1; int front = 0; int back = size - 1; int[, ] a = new int[size, size]; while (n != 0) { for (int i = front; i <= back; i++) { for (int j = front; j <= back; j++) { if (i == front || i == back || j == front || j == back) a[i, j] = n; } } ++front; --back; --n; } Print(a, size); } // function to Print pattern public static void Print(int[, ] a, int size) { for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { Console.Write(a[i, j]); } Console.WriteLine(); } } // Main Method public static void Main(string[] args) { int n = 4; // Input InnerPattern(n); } } JavaScript const MAX = 100; // function to Print pattern function prints(a, size) { for (let i = 0; i < size; i++) { let row = ''; for (let j = 0; j < size; j++) { row += a[i][j]; } console.log(row); } } // function to compute pattern function innerPattern(n) { // Pattern Size const size = 2 * n - 1; let front = 0; let back = size - 1; let a = new Array(MAX).fill(0).map(() => new Array(MAX).fill(0)); while (n !== 0) { for (let i = front; i <= back; i++) { for (let j = front; j <= back; j++) { if (i === front || i === back || j === front || j === back) { a[i][j] = n; } } } front += 1; back -= 1; n -= 1; } prints(a, size); } // Driver code // Input const n = 4; // function calling innerPattern(n); Output : 4444444 4333334 4322234 4321234 4322234 4333334 4444444 Comment More infoAdvertise with us Next Article Inner reducing pattern printing V Vignesh Waran Improve Article Tags : Misc Java Pattern Searching Matrix DSA Basic Coding Problems pattern-printing +3 More Practice Tags : JavaMatrixMiscPattern Searchingpattern-printing +1 More Similar Reads Pattern Printing Problems In many interviews star, number, and character patterns are the Most Asked Pattern Programs to check your logical and coding skills. Pattern printing programs not only improve your understanding of loops but also sharpen your coding efficiency and creativity. To solve a pattern problem, first identi 1 min read Program to print number pattern We have to print a pattern where in middle column contains only 1, right side columns contain constant digit which is greater than 1 and left side columns contains constant digit which is greater than 1. Every row should look like a Palindrome. Examples : Input : 3 Output : 1 2 1 2 1 Input : 5 Outpu 7 min read Print the given pattern recursively Given a positive integer n. Print the inverted triangular pattern (as described in the examples below) using the recursive approach. Examples: Input : n = 5 Output : * * * * * * * * * * * * * * * Input : n = 7 Output : * * * * * * * * * * * * * * * * * * * * * * * * * * * * Method 1 (Using two recur 8 min read Pattern to print X in a rectangular box Given the value of length, print the X pattern in a box using # and " " Examples: Input : 10 Output : ########## ## ## # # # # # # # # # ## # # ## # # # # # # # # # ## ## ########## Input : 7 Output : ####### ## ## # # # # # # # # # # # ## ## ####### Below is the implementation to print X in a recta 4 min read Program to print interesting pattern Program to print following pattern: ********1******** *******2*2******* ******3*3*3****** *****4*4*4*4***** ****5*5*5*5*5**** ***6*6*6*6*6*6*** **7*7*7*7*7*7*7** Examples : Input : 4 Output : ********1******** *******2*2******* ******3*3*3****** *****4*4*4*4***** Input :5 Output : ********1******** 9 min read Like