Triangular Number Patterns 1
Triangles and squares might remind you of those typical mathematical chapters that you studied in your school. Hate Maths π ? Not a problem π. These questions are more about having fun with patterns and shapes, just like connecting dots to make a picture. Here just knowing about triangles is enough to start having a blast!
Until now we covered a wide range of number pattern problems but all of them were square patterns. Now we are going to see triangular number patterns just like we did in star patterns. So let's start!
Problem 1
Input
N=5
Output
1
22
333
4444
55555
Approach
Hint 1
If you read the last article then then you might notice that this pattern is similar to one of the previous number pattern:
11111
22222
33333
44444
55555
Hence, it is recommended to go through it as it will help you to build logic for the pattern we are going to print.
Can you derive something using that code ??
Explanation/Steps
Observation: In the given problem, if we start from 1st to 5th row, the current row number gets printed in every columns. Also number of columns per row is dependent on the current row number (i.e. there is 1 column in row1, 2 in row2, 3 in row3 and so on).
Explanation:
- To iterate through rows, initialize an outer loop from 1 to N (where N is the total rows to be printed).
- To print columns per row, initialize an inner loop from 1 to current_row_number (since columns are row dependent). Inside this loop print the value of current_row_number.
Solution
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// Taking input from user
Scanner scanner = new Scanner(System.in);
System.out.println("Enter N: ");
int N = scanner.nextInt();
// Looping for every row
for (int i = 1; i <= N; i++) {
// Looping for every column of a row
for (int j = 1; j <= i; j++) {
System.out.print(i);
}
System.out.println();
}
scanner.close();
}
}
Problem 2
Input
N=5
Output
1
22
333
4444
55555
Approach
Hint
This question is very much similar to the previous problem. Just an extra twist is added regarding the spaces. Before printing the actual values, some space has to be added. Now your task is to observe how much space should be added and how can we implement that in our code?
Explanation/Steps
Observation: Number of spaces per row is in decreasing order i.e. row1 contains 4 spaces, row2 contains 3 and so on last row contains no space. If observed carefully, no. of initial spaces = N-curr_row
Explanation:
- To iterate through rows, initialize an outer loop on i from 1 to N (where N is the total rows to be printed).
- Additional step to previous problem: Run an inner loop from i (current row) to N and print space: ' '. The loop indicates the no. of trailing spaces.
- To print columns per row, initialize an inner loop from 1 to current_row_number (since columns are row dependent). Inside this loop print the value of current_row_number.
Solution
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter N: ");
int N = scanner.nextInt();
for (int i = 1; i <= N; i++) {
// Logic to print spaces
for (int j = i; j < N; j++) {
System.out.print(" ");
}
// Logic to print numbers
for (int j = 1; j <= i; j++) {
System.out.print(i);
}
System.out.println();
}
scanner.close();
}
}
Problem 3
Input
N=5
Output
55555
4444
333
22
1
Approach
Hint
This pattern is just a vertical flip version of the first problem in this article. Try to change its code applying this same logic.
Explanation/Steps
Observation: In the given problem, if we start from 1st to 5th row, the row numbers get printed in every column but in a decreasing order. Also number of columns per row is dependent on the number that's printed in the current row (i.e. there are 5 columns in row1, 4 in row2, 3 in row3 and so on).
Explanation:
- To iterate through rows, initialize an outer loop from N to 1 (where N is the total number of rows). As both 1 to N or N to 1 will iterate N times, we'll initialize the loop from N to 1 and not from 1 to N. We do this because the pattern is in descending order hence it will help in the inner loop.
- To print numbers, initialize an inner loop from 1 to current_row_number (note that the current row number will be like n.., 4, 3β¦1 in decreasing order). Inside this loop print the value of current_row_number.
Solution
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter N: ");
int N = scanner.nextInt();
for (int i = N; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
System.out.print(i);
}
System.out.println();
}
}
}
Note: This code can also be written for i ranging from 1 to N but then we would be using N-i+1 rather than i. To avoid this complexity, it's better to run a decreasing loop in such questions where decreasing pattern has to be printed.
Remember, decreasing patterns mean decreasing loops!
Approach 2
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter N: ");
int N = scanner.nextInt();
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= N - i + 1; j++) {
System.out.print(N - i + 1);
}
System.out.println();
}
}
}
Problem 4
Input
N=5
Output
11111
2222
333
44
5
Approach
Hint
If compared with the first pattern, only frequency of printed value is flipped. Rest is same!
Explanation/Steps
Observation: Observe that total columns to be printed per row is N β current_row_number + 1 i.e. first row contains 5 β 1 + 1 = 5 columns and so on. And for each column the current row number gets printed.
Explanation:
- To iterate through rows, start an outer loop from 1 to N.
- To print the numbers, start an inner loop from current row number to N. Inside this loop print the value of current row number.
Solution
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter N: ");
int N = scanner.nextInt();
for (int i = 1; i <= N; i++) {
// Logic to print numbers
for (int j = i; j <= N; j++) {
System.out.print(i);
}
System.out.println();
}
}
}
Problem 4.2
Can you decode the vertical flip version of this??? It's output would look like:
5
44
333
2222
11111
Hint
Try working on the same lines as in Problem 3.
Explanation/Steps
If you are thinking of again running the outer loop from N to 1 rather than 1 to N then yes you are right! As already said, vertical flips are same patterns but printed in an opposite order.
Solution
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter N: ");
int N = scanner.nextInt();
for (int i = N; i >= 1; i--) {
// Logic to print numbers
for (int j = i; j <= N; j++) {
System.out.print(i);
}
System.out.println();
}
}
}
Problem 5
Input
N=5
Output
1
12
123
1234
12345
Approach
Hint
Try to print the values columnwise rather than rowwise.
Explanation/Steps
Observation: There are N number of rows. Each row exactly contains i number of columns (where i is the current row number). And for each row in each column j gets printed (where j is the current column number).
Explanation:
- To iterate through rows, initialize an outer loop from 1 to N (where N is the total rows to be printed).
- To print the number, run an inner loop from 1 to i (where i is the current row number). Inside this loop print the value of j (where j is the current column number).
Solution
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter N: ");
int N = scanner.nextInt();
for (int i = 1; i <= N; i++) {
// Logic to print numbers
for (int j = 1; j <= i; j++) {
System.out.print(j);
}
System.out.println();
}
}
}
Problem 5.2
Can you print the vertical flip version now? It's output would look like:
12345
1234
123
12
1
Explanation/Steps
Run the outer loop from N to 1 rather than 1 to N.
P.S. Don't ask me why, if you have solved the above questions ; )
Solution
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter N: ");
int N = scanner.nextInt();
for (int i = N; i >= 1; i--) {
// Logic to print numbers
for (int j = 1; j <= i; j++) {
System.out.print(j);
}
System.out.println();
}
}
}
In triangular number patterns, there's an extra emphasis upon the range of inner loop. Unlike square patterns where ranges were pre-decided based on user's input of rows and columns, triangle patterns are more dependent on the current row value. We'll implement this more in our upcoming articles as well.
On this note, let's end our first article on triangular patterns problems. See you in the next article : )