Table of Contents [hide]
In this post, we will see how to print the following pyramid pattern.
Problem
Input : n = 4
Output :
1
3*2
4*5*6
10*9*8*7Input : n = 5
Output :
1
3*2
4*5*6
10*9*8*7
11*12*13*14*15
Output :
1
3*2
4*5*6
10*9*8*7Input : n = 5
Output :
1
3*2
4*5*6
10*9*8*7
11*12*13*14*15
Solution
If you notice the pattern we need to print odd rows in increasing order and even rows in decreasing order.
We will use two for loops and three variables to achieve this pattern.
Three variables:
- row: It denotes to current row
- col: It is the number which you actually prints
- num: It actually controls the number upto which you are going to print in a row.
Java program to print 1 3*2 4*5*6 pattern in java
Time Compexity: O((n (n + 1)) / 2)
That’s all about printing the pattern 1 32 456 pattern in java.
Was this post helpful?
Let us know if this post was helpful. Feedbacks are monitored on daily basis. Please do provide feedback as that\'s the only way to improve.