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++
#include <bits/stdc++.h>
using namespace std;
#define max 100
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;
}
}
void innerPattern( int n) {
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);
}
int main()
{
int n = 4;
innerPattern(n);
return 0;
}
|
Java
public class Pattern {
public static void innerPattern( int n)
{
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);
}
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();
}
}
public static void main(String[] args)
{
int n = 4 ;
innerPattern(n);
}
}
|
Python3
MAX = 100
def prints(a, size):
for i in range (size):
for j in range (size):
print (a[i][j], end = '')
print ()
def innerPattern(n):
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);
n = 4
innerPattern(n)
|
C#
using System;
public class Pattern {
public static void InnerPattern( int n)
{
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);
}
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();
}
}
public static void Main( string [] args)
{
int n = 4;
InnerPattern(n);
}
}
|
Javascript
const MAX = 100;
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 innerPattern(n) {
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);
}
const n = 4;
innerPattern(n);
|
Output :
4444444
4333334
4322234
4321234
4322234
4333334
4444444
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.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.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
02 Mar, 2023
Like Article
Save Article