Number Pattern
Problem Description: You are given with an input number N, then you have to print the given
star pattern corresponding to that number N.
For example, if N=4
Pattern output:
1 1
12 21
123 321
12344321
For N=5, the pattern output would be:
1 1
12 21
123 321
1234 4321
1234554321
Generic approach to solve pattern questions:
1. For solving pattern questions, you have to answer three questions. The first question is
how many rows are to be printed in the given pattern.
2. The second question is how many columns are to be printed in a generic row of the given
pattern.
3. The third question is what to print at generic row and generic column location.
4. The answer to these questions form the basis of implementing a pattern.
5. The generic structure of code, after answering these three questions, looks like this:
Answers to these three questions for the given pattern are:
1. The number of rows to be printed are given as input in N.
2. It can be clearly seen that each row has the same number of columns, which is 2*N.
3. In this pattern, we have to print three things: increasing numbers, spaces and decreasing
numbers
For any generic row r and generic column c,
a. Increasing numbers start from 1 and is written on columns: [1, r]
b. space is printed on columns: [r+1, 2*n-r]
c. Decreasing numbers start from r and is written on remaining columns
Pseudo code for the given problem:
input=N
i=1
while i is less than or equal to N:
j=1
num1=1
num2=i
while j is less than or equal to N:
If (j <= i)
Print num1
num1++
Else if (j>= (i+1) && j <= 2*n-i)
Print “ ”
Else
Print num2
num2--
Increment j by 1
Increment i by 1
Add a new line here