Printing patterns with numbers using nested for-loops

Key takeaways:

  • Nested loops, combining an outer loop with an inner loop, are essential for solving pattern printing problems by controlling both rows and columns.

  • Different types of patterns, such as number triangles, decrementing sequences, and alternating binary patterns, can be generated using nested loops.

  • Adjusting loop conditions—such as the number of iterations and the values printed—creates diverse patterns with varying structures and complexity.

  • Working with nested loops strengthens understanding of programming logic and control flow, essential for solving a wide range of algorithmic problems.

Nested loops are a powerful tool that unlock the ability to solve complex pattern problems efficiently. Mastering the combination of outer and inner loops enhances programming logic and paves the way for tackling more advanced challenges. With each pattern created, a stronger foundation is built for more sophisticated problem-solving in coding. They involve an outer loop encapsulating an inner loop to achieve desired patterns.

In this Answer, we will explore various printing patterns outlined below.

Basic syntax of nested loops for printing patterns

The basic syntax for using nested-for loops is given below:

for(initialization;condition;updation)
{
for(initialization;condition;updation)
{
// inner-loop statements
}
// outer-loop statements
}

To work with these patterns, it is important to understand the looping variable condition applied to it. We consider the outer loop to execute the pattern the required number of times, while the inner loop is used to print it.

10 examples of different patterns

Let’s look at some examples of printing patterns with numbers using nested for loops:

Pattern 1: Left-aligned number triangle with nested loops

Our first pattern is a “left-aligned number triangle”. Each row contains an increasing sequence of numbers starting from 1, and each row has one additional number compared to the previous row.

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

Let’s implement this pattern using the nested for loop.

#include <stdio.h>
int main() {
for(int i=1;i<=5;i++)
{
for(int j=1;j<=i;j++)
{
printf("%d ",j);
}
printf("\n");
}
}
  • Line 3: The outer loop, controlled by the variable i, determines the number of rows in the triangle. It starts from 1 and goes up to 5.

  • Line 5: The inner loop, controlled by the variable j, prints the numbers in each row. It starts from 1 and goes up to the value of i in the current iteration of the outer loop. This inner loop ensures that the numbers are printed according to the current row number.

Pattern 2: Repeated row number pattern

In pattern 2, each row consists of a repeated number corresponding to the row number.

1
2 2
3 3 3
4 4 4 4
5 5 5 5 5

Let's implement this pattern using the nested for loop.

#include <stdio.h>
int main() {
for(int i=1;i<=5;i++)
{
for(int j=1;j<=i;j++)
{
printf("%d ",i);
}
printf("\n");
}
}
  • Line 5: Initializes an inner loop where j iterates from 1 to the current value of i.

  • Line 7: During each iteration, it prints the value of i, resulting in the repetition of the current row number i times horizontally.

Pattern 3: Decreasing number triangle with nested loops

In pattern 3, each row contains decreasing numbers from the maximum value in the first row down to 1.

1 2 3 4 5
1 2 3 4
1 2 3
1 2
1

Let’s implement this pattern using the nested for loop.

#include <stdio.h>
int main() {
for(int i=5;i>=1;i--)
{
for(int j=1;j<=i;j++)
{
printf("%d ",j);
}
printf("\n");
}
}
  • Line 3: This loop handles the number of rows, starting from 5 and decrementing by 1 until it reaches 1.
  • Line 5: This loop prints the numbers from 1 to the current value of the outer loop variable i.

Pattern 4: Incrementing number start pattern

In pattern 4, each row begins with a particular number and increments until it reaches the maximum number (5).

1 2 3 4 5
2 3 4 5
3 4 5
4 5
5

Let’s implement this pattern using the nested for loop.

#include <stdio.h>
int main() {
for(int i=1;i<=5;i++)
{
for(int j=i;j<=5;j++)
{
printf("%d ",j);
}
printf("\n");
}
}

In the above code, the outer loop iterates from 1 to 5. For each iteration of this loop, an inner loop is used to print the numbers starting from the current value of the outer loop variable to 5. For example, the inner loop starts from 1, in the second row from 2, and so on.

Pattern 5: Decrementing numbers starting from 5

In pattern 5, each row starts with 5 and decrements by 1 until it reaches the number dictated by its row number.

5
5 4
5 4 3
5 4 3 2
5 4 3 2 1

Let's implement this pattern using the nested for loop.

#include <stdio.h>
int main() {
for(int i=5;i>=1;i--)
{
for(int j=5;j>=i;j--)
{
printf("%d ",j);
}
printf("\n");
}
}
  • Line 3: The outer loop handles the number of rows printed in the pattern. It starts from 5 and decrements i by 1 in each iteration until i becomes 1.

  • Line 5: The inner loop handles the number of columns or the values printed in each row. It starts from 5 and decrements j by 1 in each iteration until j becomes equal to the current value of i.

Pattern 6: Alternating binary pattern

This pattern is simply an extension of Pattern 1. This pattern is obtained by performing the %2 operation on each element.

1
1 0
1 0 1
1 0 1 0
1 0 1 0 1

Let's implement this pattern using the nested for loop.

#include <stdio.h>
int main() {
for(int i=1;i<=5;i++)
{
for(int j=1;j<=i;j++)
{
printf("%d ",j%2);
}
printf("\n");
}
}
  • Lines 5–8: This inner loop initializes j to 1 and iterates as long as j is less than or equal to the current value of i. In each iteration, it prints the value of j % 2. The j % 2 calculates the remainder of j divided by 2. If j is an even number, j % 2 will result in 0. If j is an odd number, j % 2 will result in 1.

Pattern 7: Consecutive numbers in rows

Pattern 7 consists of consecutive numbers where each row contains one more number than the previous row, starting from 1 and increasing consecutively.

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

Let's implement this pattern using the nested for loop.

#include<stdio.h>
int main()
{
int i,j,k=1;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf("%d ",k++);
}
printf("\n");
}
}
  • Line 4: Initializes k to keep track of the consecutive numbers to be printed in the pattern and ensures that the numbers are incremented sequentially on line 9.
  • Line 5: The outer loop handles the number of rows in the pattern. It starts with i = 1 and continues up to 5, incrementing i by 1 in each iteration.
  • Line 6: The inner loop handles the number of columns in each row of the pattern. It starts with j = 1 and continues up to the current value of i.

Pattern 8: Binary alternating pattern with consecutive numbers

We get this pattern by performing the %2 operation on Pattern 7. Let’s implement this pattern using the nested for loop.

1
0 1
0 1 0
1 0 1 0
1 0 1 0 1

#include<stdio.h>
int main()
{
int i,j,k=1;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf("%d ",(k++)%2);
}
printf("\n");
}
}
  • Lines 7–10: The inner loop handles the number of columns or the values printed in each row. It prints either 0 or 1 for i times depending on the value of k at that moment. The value of k is increased by 1 following each inner loop iteration.

Pattern 9: Right-aligned triangle pattern with spaces

In pattern 9, the triangle is right-aligned. We need to add the required number of spaces in a given pattern. Here, we add another loop to add the required number of spaces.

        1
     1 2
       1 2 3
  1 2 3 4
  1 2 3 4 5

Let’s implement this pattern using the nested-for loop.

#include <stdio.h>
int main()
{
int i,j,k;
for(i=1;i<=5;i++)
{
for(k=5;k>=i;k--)
{
printf(" ");
}
for(j=1;j<=i;j++)
{
printf("%d",j);
}
printf("\n");
}
}
  • Line 5: Declare three integer variables i, j, and k to be used in the loops.

  • Line 6: This loop handles the number of rows to be printed. It iterates from 1 to 5.

  • Lines 8–10: This loop prints spaces before the numbers in each row. The number of spaces decreases with each row.

  • Line 12: This loop prints the numbers in each row, starting from 1 and going up to the current value of i.

Pattern 10: Number pyramid with spaces between elements

In pattern 10, each row starts with the number 1 and increments by 1 for each subsequent element in the row. We get this pattern by adding a space to Pattern 9.

        1
      1   2
       1   2   3
  1   2   3   4
  1   2   3   4   5

Let’s implement this pattern using the nested for loop.

#include <stdio.h>
int main()
{
int i,j,k;
for(i=1;i<=5;i++)
{
for(k=5;k>=i;k--)
{
printf(" ");
}
for(j=1;j<=i;j++)
{
printf("%d ",j);
}
printf("\n");
}
}

The provided code corresponds to pattern 9. The only modification occurs on line 14, where the printf statement outputs the current value of j followed by a space.

Ready to level up your coding skills? The Beginner to Advanced Computing and Logic Building course takes you from the fundamentals to advanced problem-solving, including creating games and FAANG interview prep.

Conclusion

Nested loops are an essential concept for solving printing pattern problems in programming. Understanding how to control the outer and inner loops helps in creating intricate patterns, whether for simple number sequences or complex shapes.

Frequently asked questions

Haven’t found what you were looking for? Contact Us


How to make a star pattern in Python

Use loops to print stars in various shapes. For example, a simple triangle pattern:

j = 5
for i in range(1, j+1):
    print('*' * i)

How to print an alphabet pattern in Python?

Use loops to print alphabets. For example, a pattern of letters:

j = 5
alphabets = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
for i in range(j):
    print(alphabets[i] * (i+1))


What is a nested for loop in programming?

A nested for loop is a loop inside another loop. In the context of printing patterns, the outer loop controls the rows, while the inner loop controls the columns.


Free Resources