Skip to main content

Control Flow Practice

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

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 ??

Logic

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:

  1. To iterate through rows, initialize an outer loop from 1 to N (where N is the total rows to be printed).
  2. 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.

Try it yourself

Solution
#include <iostream>
using namespace std;

int main() {
    int i, j, N;

    cout << "Enter N: "<<endl;
    cin >> N;

    for (i = 1; i <= N; i++) {
        for (j = 1; j <= i; j++) {
            cout << i;
        }

        cout << "\n";
    }

    return 0;
}

Problem 2

Input

N=5

Output

    1
   22
  333
 4444
55555

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?

Logic

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:

  1. To iterate through rows, initialize an outer loop on i from 1 to N (where N is the total rows to be printed).
  2. 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.
  3. 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.

Try it yourself

Solution
#include <iostream>
using namespace std;

int main() {
    int i, j, N;

    cout << "Enter N: "<<endl;
    cin >> N;

    for (i = 1; i <= N; i++) {
        // Logic to print spaces
        for (j = i; j < N; j++) {
            cout << " ";
        }

        // Logic to print numbers
        for (j = 1; j <= i; j++) {
            cout << i;
        }

        cout << "\n";
    }

    return 0;
}

Problem 3

Input

N=5

Output

55555
4444
333
22
1

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.

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 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:

  1. 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.
  2. 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.
💡
For any vertical flip question, simply reverse the outer loop. This means if the original pattern's loop is from 1 to N then make it N to 1 or vice versa and the code for vertically flipped pattern is ready🥳

Try it yourself

Solution
#include <iostream>
using namespace std;

int main() {
    int i, j, N;

    cout << "Enter N: ";
    cin >> N;

    for (i = N; i >= 1; i--) {
        for (j = 1; j <= i; j++) {
            cout << i;
        }

        cout << "\n";
    }

    return 0;
}

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!

Try it yourself

Approach 2
#include <iostream>
using namespace std;

int main() {
    int i, j, N;

    cout << "Enter N: "<<endl;
    cin >> N;

    for (i = 1; i <= N; i++) {
        for (j = 1; j <= N-i+1; j++) {
            cout << N-i+1;
        }

        cout << "\n";
    }

    return 0;
}

Problem 4

Input

N=5

Output

11111
2222
333
44
5

Hint

If compared with the first pattern, only frequency of printed value is flipped. Rest is same!

Logic

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:

  1. To iterate through rows, start an outer loop from 1 to N.
  2. To print the numbers, start an inner loop from current row number to N. Inside this loop print the value of current row number.

Try it yourself

Solution
#include <iostream>
using namespace std;

int main() {
    int N;

    cout << "Enter N: ";
    cin >> N;

    for (int i = 1; i <= N; i++) {
        // Logic to print numbers
        for (int j = i; j <= N; j++) {
            cout << i;
        }

        cout << endl;
    }

    return 0;
}

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.

Logic

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.

Try it yourself

Solution
#include <iostream>
using namespace std;

int main() {
    int N;

    cout << "Enter N: "<<endl;
    cin >> N;

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

        cout << endl;
    }

    return 0;
}

Problem 5

Input

N=5

Output

1
12
123
1234
12345

Hint

Try to print the values columnwise rather than rowwise.

Logic

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:

  1. To iterate through rows, initialize an outer loop from 1 to N (where N is the total rows to be printed).
  2. 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).

Try it yourself

Solution
#include <iostream>
using namespace std;

int main() {
    int N;

    cout << "Enter N: " << endl;
    cin >> N;

    for (int i = 1; i <= N; i++) {
        // Logic to print numbers
        for (int j = 1; j <= i; j++) {
            cout << j;
        }

        cout << endl;
    }

    return 0;
}

Problem 5.2

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

12345
1234
123
12
1

Logic

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 ; )

Try it yourself

Solution
#include <iostream>
using namespace std;

int main() {
    int N;

    cout << "Enter N: " << endl;
    cin >> N;

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

        cout << endl;
    }

    return 0;
}

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 : )