You are on page 1of 13

4/22/2020 How to learn Pattern printing easily?

- GeeksforGeeks

es

How to learn Pattern printing easily?
The task is to print a pattern as shown in the example for a given integer value.
Custom Search
The goal is not to print just this one pattern, it is to learn the best approach to solve this kind of
in problems as these questions are frequently asked in coding exams and in job interviews.
Hire with us!
Examples:

Input: N = 4
Output:
4 4 4 4 4 4 4
4 3 3 3 3 3 4
4 3 2 2 2 3 4
4 3 2 1 2 3 4
4 3 2 2 2 3 4
4 3 3 3 3 3 4
4 4 4 4 4 4 4

Input: N = 7
Output:
7 7 7 7 7 7 7 7 7 7 7 7 7
7 6 6 6 6 6 6 6 6 6 6 6 7
7 6 5 5 5 5 5 5 5 5 5 6 7
7 6 5 4 4 4 4 4 4 4 5 6 7
7 6 5 4 3 3 3 3 3 4 5 6 7
7 6 5 4 3 2 2 2 3 4 5 6 7
7 6 5 4 3 2 1 2 3 4 5 6 7
7 6 5 4 3 2 2 2 3 4 5 6 7 ▲
7 6 5 4 3 3 3 3 3 4 5 6 7
7 6 5 4 4 4 4 4 4 4 5 6 7
7 6 5 5 5 5 5 5 5 5 5 6 7

https://www.geeksforgeeks.org/how-to-learn-pattern-printing-easily/ 1/13
4/22/2020 How to learn Pattern printing easily? - GeeksforGeeks

7 6 6 6 6 6 6 6 6 6 6 6 7
7 7 7 7 7 7 7 7 7 7 7 7 7

For the tutorial, an example of N = 4 is used.

Step 1: First of all, analyse the pattern for any lines of symmetry. Here our pattern is both
vertically and horizontally symmetrical, so draw the lines of symmetry like this,

After breaking the pattern in parts, rst try to draw only the upper-left part, namely, part A. If there
is not any line of symmetry, jump to Step 2.

Step 2: Now associate each cell i.e. element with a row and column no.( usually denoted by i and
j, respectively) Just like,

https://www.geeksforgeeks.org/how-to-learn-pattern-printing-easily/ 2/13
4/22/2020 How to learn Pattern printing easily? - GeeksforGeeks

From now on, a cell is denoted by C(i, j) with its row and column no.

Step 3: In this step, try and nd a relation between the value of C(i, j) with i and/or j. Usually, the
value of C depends on the value of N and values of i and j relative to each other. To elaborate,
– In 1st row, every element is same i.e. 4(=N). So it is not of much help.
– In 2nd row, C can be seen decreasing from of 4 to 3 for i>=j and then it stays 3 for next i=j and
then it stays 2 for next i=j for all values of j, C can be seen decreasing from 4 to 1.

So in every row, C starts from N, decreases by 1 until i>=j, and then becomes constant. Here the
value of C depends upon the smaller between i and j and the formula could be:

C(i, j) = N - min(i, j) + 1

So our approach should be:

C++

#include <iostream>
using namespace std;

int main()
{

https://www.geeksforgeeks.org/how-to-learn-pattern-printing-easily/ 3/13
4/22/2020 How to learn Pattern printing easily? - GeeksforGeeks

int N = 4, i, j, min;
cout << "Value of N: " << N << endl;

for (i = 1; i <= N; i++) {


for (j = 1; j <= N; j++) {
min = i < j ? i : j;
cout << N - min + 1;
}
cout << endl;
}
return 0;
}

Java

// Java progarm to find triplets


// a[i]>a[j]>a[k] and i<j<k
import java.util.*;

class GFG
{

// Driver code
public static void main(String[] args)
{
int N = 4, i, j, min;
System.out.println("Value of N: " + N);

for (i = 1; i <= N; i++)


{
for (j = 1; j <= N; j++)
{
min = i < j ? i : j;
System.out.print(N - min + 1);
}
System.out.println();
}
}
}

// This code is contributed by Princi Singh

Python3

# Python3 progarm to find triplets


# a[i]>a[j]>a[k] and i<j<k
if __name__ == '__main__':

N = 4;
print("Value of N: ", N);

for i in range(1, N + 1):


for j in range(1, N + 1):
min = i if i < j else j; ▲
print(N - min + 1, end = "");
print();

# This code is contributed


# by PrinciRaj1992
https://www.geeksforgeeks.org/how-to-learn-pattern-printing-easily/ 4/13
4/22/2020 How to learn Pattern printing easily? - GeeksforGeeks

C#

// C# progarm to find triplets


// a[i]>a[j]>a[k] and i<j<k
using System;

class GFG
{

// Driver code
public static void Main(String[] args)
{
int N = 4, i, j, min;
Console.WriteLine("Value of N: " + N);

for (i = 1; i <= N; i++)


{
for (j = 1; j <= N; j++)
{
min = i < j ? i : j;
Console.Write(N - min + 1);
}
Console.WriteLine();
}
}
}

// This code is contributed by PrinciRaj1992

Output:

Value of N: 4
4444
4333
4322
4321

If the pattern didn’t have any symmetry and is now completed, the work is done. But for patterns
with symmetry, they are still incomplete and it is required to go to step 4.

Step 4: Now include part B of the pattern in the picture and associate those elements with column
numbers. But don’t assign column numbers serial-wise, instead, assign them the same column
number that was assigned to its mirror column in part A.

https://www.geeksforgeeks.org/how-to-learn-pattern-printing-easily/ 5/13
4/22/2020 How to learn Pattern printing easily? - GeeksforGeeks

Step 5: Now change the code to append part B. To do that, it is just needed to rerun the inner loop
for j = n-1 to j=1.

C++

#include <iostream>
using namespace std;

int main()
{
int N = 4, i, j, min;
cout << "Value of N: " << N << endl;

for (i = 1; i <= N; i++) {


for (j = 1; j <= N; j++) {
min = i < j ? i : j;
cout << N - min + 1;
}
for (j = N - 1; j >= 1; j--) {
min = i < j ? i : j;
cout << N - min + 1;
}
cout << endl;
}
return 0;
}

Java

class GFG
{
public static void main(String[] args) ▲
{
int N = 4, i, j, min;
System.out.println("Value of N: " + N);

for (i = 1; i <= N; i++)

https://www.geeksforgeeks.org/how-to-learn-pattern-printing-easily/ 6/13
4/22/2020 How to learn Pattern printing easily? - GeeksforGeeks

{
for (j = 1; j <= N; j++)
{
min = i < j ? i : j;
System.out.print(N - min + 1);
}
for (j = N - 1; j >= 1; j--)
{
min = i < j ? i : j;
System.out.print(N - min + 1);
}
System.out.println();
}
}
}

// This code is contributed by PrinciRaj1992

Python3

N = 4;
print("Value of N: ", N);

for i in range(1, N + 1):


for j in range(1, N + 1):
min = i if i < j else j;
print(N - min + 1, end = "");

for j in range(N - 1, 0, -1):


min = i if i < j else j;
print(N - min + 1, end = "");
print();

# This code is contributed by Rajput-Ji

C#

using System;

class GFG
{
public static void Main(String[] args)
{
int N = 4, i, j, min;
Console.WriteLine("Value of N: " + N);

for (i = 1; i <= N; i++)


{
for (j = 1; j <= N; j++)
{
min = i < j ? i : j;
Console.Write(N - min + 1);
}
for (j = N - 1; j >= 1; j--) ▲
{
min = i < j ? i : j;
Console.Write(N - min + 1);
}
Console.WriteLine();
https://www.geeksforgeeks.org/how-to-learn-pattern-printing-easily/ 7/13
4/22/2020 How to learn Pattern printing easily? - GeeksforGeeks

}
}
}

// This code is contributed by Rajput-Ji

Output:

Value of N: 4
4444444
4333334
4322234
4321234

Step 6: Now includes part C and D of the pattern in the picture and associate those elements with
row numbers in the same way the column numbers are assigned in step 4.

Step 7: Now, In the same way, as done in step 5, change the code to append part C and D. To do
that, one just need to rerun the outer loop for i = n-1 to i=1. ▲

C++

#include <iostream>
https://www.geeksforgeeks.org/how-to-learn-pattern-printing-easily/ 8/13
4/22/2020 How to learn Pattern printing easily? - GeeksforGeeks

using namespace std;

int main()
{
int N = 4, i, j, min;
cout << "Value of N: " << N << endl;

for (i = 1; i <= N; i++) {


for (j = 1; j <= N; j++) {
min = i < j ? i : j;
cout << N - min + 1;
}
for (j = N - 1; j >= 1; j--) {
min = i < j ? i : j;
cout << N - min + 1;
}
cout << endl;
}
for (i = N - 1; i >= 1; i--) {
for (j = 1; j <= N; j++) {
min = i < j ? i : j;
cout << N - min + 1;
}
for (j = N - 1; j >= 1; j--) {
min = i < j ? i : j;
cout << N - min + 1;
}
cout << endl;
}
return 0;
}

Java

// Java implementation of the approach


class GFG
{
public static void main(String[] args)
{
int N = 4, i, j, min;
System.out.println("Value of N: " + N);

for (i = 1; i <= N; i++)


{
for (j = 1; j <= N; j++)
{
min = i < j ? i : j;
System.out.print(N - min + 1);
}
for (j = N - 1; j >= 1; j--)
{
min = i < j ? i : j;
System.out.print(N - min + 1);
}
System.out.println();
} ▲

for (i = N - 1; i >= 1; i--)


{
for (j = 1; j <= N; j++)
{

https://www.geeksforgeeks.org/how-to-learn-pattern-printing-easily/ 9/13
4/22/2020 How to learn Pattern printing easily? - GeeksforGeeks

min = i < j ? i : j;
System.out.print(N - min + 1);
}
for (j = N - 1; j >= 1; j--)
{
min = i < j ? i : j;
System.out.print(N - min + 1);
}
System.out.println();
}
}
}

// This code is contributed by 29AjayKumar

C#

// C# implementation of the approach


using System;

class GFG
{
public static void Main(String[] args)
{
int N = 4, i, j, min;
Console.WriteLine("Value of N: " + N);

for (i = 1; i <= N; i++)


{
for (j = 1; j <= N; j++)
{
min = i < j ? i : j;
Console.Write(N - min + 1);
}
for (j = N - 1; j >= 1; j--)
{
min = i < j ? i : j;
Console.Write(N - min + 1);
}
Console.WriteLine();
}

for (i = N - 1; i >= 1; i--)


{
for (j = 1; j <= N; j++)
{
min = i < j ? i : j;
Console.Write(N - min + 1);
}
for (j = N - 1; j >= 1; j--)
{
min = i < j ? i : j;
Console.Write(N - min + 1);
}
Console.WriteLine();
} ▲
}
}

// This code is contributed by Rajput-Ji

https://www.geeksforgeeks.org/how-to-learn-pattern-printing-easily/ 10/13
4/22/2020 How to learn Pattern printing easily? - GeeksforGeeks

Output:

Value of N: 4
4444444
4333334
4322234
4321234
4322234
4333334
4444444

Recommended Posts:
Inner reducing pattern printing
Printing Heart Pattern in C
Printing Triangle Pattern in Java
Printing the Triangle Pattern using last term N
Printing string in plus ‘+’ pattern in the matrix
Program for Expressionless Face Pattern printing
Pattern Printing question asked in CGI Coding Round
PHP programs for printing pyramid patterns
Programs for printing pyramid patterns in C++
Programs for printing pyramid patterns using recursion
Programs for printing pyramid patterns in Python
Programs for printing pyramid patterns in Java
Object Oriented Programming in Python | Set 2 (Data Hiding and Object Printing)
Magical Pattern ▲
Spiral Pattern

https://www.geeksforgeeks.org/how-to-learn-pattern-printing-easily/ 11/13
4/22/2020 How to learn Pattern printing easily? - GeeksforGeeks

Parresh77
Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using
contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article
appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you nd anything incorrect by clicking on the "Improve Article" button
below.

Improved By : 29AjayKumar, Rajput-Ji, princi singh, princiraj1992

Article Tags : Analysis School Programming pattern-printing

Practice Tags : pattern-printing


12

3
To-do Done
Based on 6 vote(s)

Feedback/ Suggest Improvement Add Notes Improve Article

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.

Load Comments

https://www.geeksforgeeks.org/how-to-learn-pattern-printing-easily/ 12/13
4/22/2020 How to learn Pattern printing easily? - GeeksforGeeks

5th Floor, A-118,


Sector-136, Noida, Uttar Pradesh - 201305
feedback@geeksforgeeks.org

COMPANY LEARN
About Us Algorithms
Careers Data Structures
Privacy Policy Languages
Contact Us CS Subjects
Video Tutorials

PRACTICE CONTRIBUTE
Courses Write an Article
Company-wise Write Interview Experience
Topic-wise Internships
How to begin? Videos

@geeksforgeeks, Some rights reserved

https://www.geeksforgeeks.org/how-to-learn-pattern-printing-easily/ 13/13

You might also like