Skip to main content

Control Flow: Practice

Triangular Number Patterns 2

Hello coders : )

I hope last articles gave you some basic practice upon triangular patterns. In this article we are going to practice a few more problems upon triangular number patterns. But before proceeding towards them remember the 2 key points for number pattern problems:

- Decreasing patterns mean decreasing loops
- Reverse the outer loop to get the vertical flip of a given pattern

Problem 1

Input

N=5

Output

1
21
321
4321
54321

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:

1
12
123
1234
12345

Hint 2

If you reverse each row of the pattern given in hint 1, you'll obtain the pattern given in this problem!

Logic

Observation: The pattern consists of N rows (where N is the total number of rows to be printed). Each row contains exactly i columns (where i is the current row number). The columns are printed in descending order starting from current row number.

Explanation:

  1. To iterate through rows, initialize an outer loop from 1 to N (where N is the total rows to be printed).
  2. Since the columns are printed in descending order hence, you must run the loop on j from i to 1 rather than 1 to i. Inside this loop print the value of j (where j is the current column number in decreasing order).

Try it yourself here!

Solution
import java.util.Scanner;

public class PrintNumbersInDecreasingOrder {
    public static void main(String[] args) {
        int N;

        System.out.println("Enter N:");
        Scanner scanner = new Scanner(System.in);
        N = scanner.nextInt();

        for (int i = 1; i <= N; i++) {
            // Logic to print numbers
            for (int j = i; j >= 1; j--) {
                System.out.print(j);
            }

            System.out.println();
        }
    }
}

Problem 1.2

Can you print the vertical flip version now? It's output would look like:

54321
4321
321
21
1

Logic

If you have solved the previous article, then you must know the shortcut for such vertical flips: You just have to reverse the outer loop.

Here the outer loop runs from 1 to N, so for flipping the given pattern, you just need to change the outer loop from N to 1.

Try it yourself here!

Solution
import java.util.Scanner;

public class PrintNumbersInDecreasingOrder {
    public static void main(String[] args) {
        int N;

        System.out.println("Enter N:");
        Scanner scanner = new Scanner(System.in);
        N = scanner.nextInt();

        for (int i = N; i >= 1; i--) {
            // Logic to print numbers
            for (int j = i; j >= 1; j--) {
                System.out.print(j);
            }

            System.out.println();
        }
    }
}

Problem 2

Input

N=5

Output

5
54
543
5432
54321

Hint

Here decreasing patterns are used 2 times: for rows and columns both.

Logic

Observation: Pattern printing is in from row 1 to N is in descending order. Number of columns in each row = current row number. Pattern printing of columns is also in decreasing order starting from N going till current row no. from the opposite end (5 for row 1 and 1 for row 5).

Explanation:

  1. To iterate through rows, run an outer loop on i from N to 1. Since the pattern is printed in descending order therefore we have initialized the loop from N and iterate till 1.
  2. To print the columns, run an inner loop on j from N to i (i is current row no. but from the opposite end as the outer loop on i is from N to 1 and not 1 to N).
  3. Inside this loop print the value of j (where j is the current column number). Since, we are also printing column values in descending order, inner loop is also from N to i rather than i to N.

Try it yourself here!

Solution
import java.util.Scanner;

public class PrintIncrementalNumbers {
    public static void main(String[] args) {
        int N;

        System.out.println("Enter N:");
        Scanner scanner = new Scanner(System.in);
        N = scanner.nextInt();

        for (int i = 1; i <= N; i++) {
            int k = i; // k = current row no.

            // k increments with each column
            for (int j = 1; j <= i; j++, k++) {
                System.out.print(k);
            }

            System.out.println();
        }
    }
}

Problem 2.2

Now again we have to print the vertical flip version of this pattern. The pattern would be:

54321
5432
543
54
5

Logic

You might have remembered the logic for such vertical flips: You just have to reverse the outer loop.

Here the outer loop runs from N to 1, so we'll flip the given pattern, and change the outer loop from 1 to N.

Solution
import java.util.Scanner;

public class PrintDescendingNumbers {
    public static void main(String[] args) {
        int N;

        // Input N from the user
        System.out.println("Enter N: ");
        Scanner scanner = new Scanner(System.in);
        N = scanner.nextInt();

        for (int i = 1; i <= N; i++) {
            // Logic to print numbers
            for (int j = N; j >= i; j--) {
                System.out.print(j);
            }

            System.out.println();
        }
    }
}

Problem 3

Input

N=5

Output

5
45
345
2345
12345

Hint

This problem is very similar to the previous problem 2. Here each rows are reversed, rest everthing is same! Saying in simpler terms, rows follow a decreasing pattern and columns follow an increasing pattern unlike the previous pattern where both followed a decreasing pattern.

Logic

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 depends on the current row no.

Explanation:

  1. To iterate through rows, run an outer loop on i from N to 1 in decreasing order.
  2. To print columns, run an inner loop from i to N (don't forget that i is running in reverse order). Inside this loop print the value of j (values from i to N indicating the column number).

Note that the outer loop is from N to 1 as the pattern is decreasing with increasing row. But the inner loop is increasing from i to N as when you observe the column values in any row, the values are in increasing order.

Try it yourself

Solution
import java.util.Scanner;

public class PrintAscendingNumbers {
    public static void main(String[] args) {
        int i, j, N;

        // Input N from the user
        System.out.println("Enter N: ");
        Scanner scanner = new Scanner(System.in);
        N = scanner.nextInt();

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

            System.out.println();
        }
    }
}

Problem 3.2

Now again we have to print the vertical flip version of this pattern. The pattern would be:

12345
2345
345
45
5

Logic

You might have remembered the logic for such vertical flips: You just have to reverse the outer loop.

Here the outer loop runs from N to 1, so we'll flip the given pattern, and change the outer loop from 1 to N.

Try it yourself

Solution
import java.util.Scanner;

public class PrintAscendingNumbers {
    public static void main(String[] args) {
        int i, j, N;

        // Input N from the user
        System.out.println("Enter N: ");
        Scanner scanner = new Scanner(System.in);
        N = scanner.nextInt();

        for (i = 1; i <= N; i++) {
            for (j = i; j <= N; j++) {
                System.out.print(j);
            }

            System.out.println();
        }
    }
}

Problem 4

Input

N=5

Output

1
23
345
4567
56789

Hint

You need another variable to keep track of values to be printed.

Logic

Observation: If you notice the above pattern consists of N rows (where N is the total number of rows to be printed). Each row contains i columns (where i is the current row number). In each row, the value starts from current row number and is incremented i-1 times.

Explanation:

  1. To iterate through rows, run an outer loop from 1 to N.
  2. Inside the outer loop initialize k = i (where k is an extra variable which will hold the number which we need to print next and i is the current row number).
  3. To print the numbers, run an inner loop from 1 to i as i numbers are printed in ith row. Inside this loop print the value of k. Also increment the value of k after printing.

Try it yourself

Solution
import java.util.Scanner;

public class PrintPattern {
    public static void main(String[] args) {
        int N;

        // Input N from the user
        System.out.println("Enter N: ");
        Scanner scanner = new Scanner(System.in);
        N = scanner.nextInt();

        for (int i = 1; i <= N; i++) {
            int k = i; // k = current row no.

            // k increments with each column
            for (int j = 1; j <= i; j++, k++) {
                System.out.print(k);
            }

            System.out.println();
        }
    }
}

Problem 4.2

Now again we have to print the vertical flip version of this pattern. The pattern would be:

56789
4567
345
23
1

Logic

You must be knowing the logic for such vertical flips by now: Reverse the outer loop.

Here the outer loop runs from 1 to N, so we'll flip the given pattern, and change the outer loop from N to 1.

Try it yourself here!

Solution
import java.util.Scanner;

public class PrintIncrementalNumbers {
    public static void main(String[] args) {
        int N;

        System.out.println("Enter N:");
        Scanner scanner = new Scanner(System.in);
        N = scanner.nextInt();

        for (int i = N; i >= 1; i--) {
            int k = i;

            // Logic to print numbers
            for (int j = 1; j <= i; j++, k++) {
                System.out.print(k);
            }

            System.out.println();
        }
    }
}

Problem 5

Input

N=5

Output

13579
3579
579
79
9

Hint

Here also the values are incremented in every row but emphasise on the row's starting value and the pattern of incrementing the values.

Logic

Observation:

  • Pattern only consists of odd numbers.
  • Each row contains exactly N – i + 1 columns (where i is the current row number).
  • For each row odd number starts with the expression i * 2 – 1.
  • Values are incremented by 2.

Implementation:

  1. To iterate through rows, run an outer loop from 1 to N.
  2. Inside this outer loop, initialize variable k = i * 2 – 1 (where k is used to keep track of next odd number to be printed).
  3. To iterate though columns, run an inner loop from i to N (where i is the current row number).
    Inside this loop print the value of k and increment it to k = k + 2.

Try it yourself here!

Solution
import java.util.Scanner;

public class PrintOddNumbers {
    public static void main(String[] args) {
        int N;

        System.out.println("Enter N:");
        Scanner scanner = new Scanner(System.in);
        N = scanner.nextInt();

        for (int i = 1; i <= N; i++) {
            int k = (i * 2) - 1;

            // Logic to print numbers
            for (int j = i; j <= N; j++, k += 2) {
                System.out.print(k);
            }

            System.out.println();
        }
    }
}

Problem 5.2

Now again we have to print the vertical flip version of this pattern. The pattern would be:

9
79
579
3579
13579

Logic

You must be knowing the logic for such vertical flips by now: Reverse the outer loop.

Here the outer loop runs from N to 1, so we'll flip the given pattern, and change the outer loop from 1 to N.

Try it yourself here!

Solution
import java.util.Scanner;

public class PrintOddNumbers {
    public static void main(String[] args) {
        int N;

        System.out.println("Enter N:");
        Scanner scanner = new Scanner(System.in);
        N = scanner.nextInt();

        for (int i = N; i >= 1; i--) {
            int k = (i * 2) - 1;

            // Logic to print numbers
            for (int j = i; j <= N; j++, k += 2) {
                System.out.print(k);
            }

            System.out.println();
        }
    }
}

With this, we come to an end for this article but not to the end of tringular number patterns. In the next article, we would be learning about another type of triangular number pattern. Till then keep practicing : )