Nested Loops and Pattern Problems in C Practice Questions with Solutions

Introduction

Nested loops are loops placed inside another loop. They are especially useful for solving pattern problems, working with rows and columns, and processing tables or grids. In this chapter, you will practice nested for loops through 10 progressively designed examples. You will start with simple rectangle patterns and move toward triangles, number patterns, inverted patterns, and practical row-column problems. These examples will help you build the loop logic needed for arrays, matrices, and advanced C programs. Nested Loops and Pattern Problems in C Practice questions with solutions to help you understand the concepts.

Q1. Print a Rectangle of Stars

Problem Statement

Write a C program to print a rectangle containing 4 rows and 5 stars in each row.

Expected pattern:

*****
*****
*****
*****

C Program

#include <stdio.h>

int main()
{
    int i, j;

    for (i = 1; i <= 4; i++)
    {
        for (j = 1; j <= 5; j++)
        {
            printf("*");
        }

        printf("\n");
    }

    return 0;
}

Sample Output

*****
*****
*****
*****

Explanation

There are two loops.

The outer loop controls rows:

for (i = 1; i <= 4; i++)

The inner loop controls stars in each row:

for (j = 1; j <= 5; j++)

For every row, the inner loop prints 5 stars.

Concepts Covered

  • Nested for loop
  • Rows and columns
  • Pattern printing
  • Inner loop
  • Outer loop

Q2. Print a Square of Numbers

Problem Statement

Write a C program to print the following pattern:

1234
1234
1234
1234

C Program

#include <stdio.h>

int main()
{
    int i, j;

    for (i = 1; i <= 4; i++)
    {
        for (j = 1; j <= 4; j++)
        {
            printf("%d", j);
        }

        printf("\n");
    }

    return 0;
}

Sample Output

1234
1234
1234
1234

Explanation

The outer loop creates 4 rows.

The inner loop prints:

1 2 3 4

for every row.

The value of j starts from 1 again whenever a new row begins.

Concepts Covered

  • Nested loops
  • Number patterns
  • Row-column logic
  • printf()

Q3. Print an Increasing Star Triangle

Problem Statement

Write a C program to print:

*
**
***
****
*****

C Program

#include <stdio.h>

int main()
{
    int i, j;

    for (i = 1; i <= 5; i++)
    {
        for (j = 1; j <= i; j++)
        {
            printf("*");
        }

        printf("\n");
    }

    return 0;
}

Sample Output

*
**
***
****
*****

Explanation

The important part is:

j <= i

When i = 1, the inner loop prints 1 star.

When i = 2, it prints 2 stars.

When i = 3, it prints 3 stars.

So the number of stars increases with every row.

Concepts Covered

  • Nested loops
  • Increasing pattern
  • Row-dependent condition
  • Pattern logic

Q4. Print an Inverted Star Triangle

Problem Statement

Write a C program to print:

*****
****
***
**
*

C Program

#include <stdio.h>

int main()
{
    int i, j;

    for (i = 5; i >= 1; i--)
    {
        for (j = 1; j <= i; j++)
        {
            printf("*");
        }

        printf("\n");
    }

    return 0;
}

Sample Output

*****
****
***
**
*

Explanation

The outer loop starts from 5 and decreases:

5 → 4 → 3 → 2 → 1

The inner loop prints the number of stars represented by i.

Therefore, every new row contains one fewer star.

Concepts Covered

  • Nested loops
  • Reverse loop
  • Decreasing pattern
  • i--

Q5. Print a Number Triangle

Problem Statement

Write a C program to print:

1
12
123
1234
12345

C Program

#include <stdio.h>

int main()
{
    int i, j;

    for (i = 1; i <= 5; i++)
    {
        for (j = 1; j <= i; j++)
        {
            printf("%d", j);
        }

        printf("\n");
    }

    return 0;
}

Sample Output

1
12
123
1234
12345

Explanation

The outer loop controls the number of rows.

The inner loop starts at 1 for every row and continues until j <= i.

For example, when:

i = 4

the inner loop prints:

1234

Concepts Covered

  • Nested loops
  • Number pattern
  • Row-dependent loop
  • Incrementing numbers

Q6. Print the Same Number in Each Row

Problem Statement

Write a C program to print:

1
22
333
4444
55555

C Program

#include <stdio.h>

int main()
{
    int i, j;

    for (i = 1; i <= 5; i++)
    {
        for (j = 1; j <= i; j++)
        {
            printf("%d", i);
        }

        printf("\n");
    }

    return 0;
}

Sample Output

1
22
333
4444
55555

Explanation

This pattern is slightly different from the previous example.

Previously, we printed j:

printf("%d", j);

Here, we print i:

printf("%d", i);

Therefore, every row contains the same number.

For example:

i = 3

The inner loop runs three times and prints:

333

Concepts Covered

  • Nested loops
  • Difference between i and j
  • Number patterns
  • Row-based values

Q7. Print a Hollow Rectangle

Problem Statement

Write a C program to print a hollow rectangle of 5 rows and 6 columns.

Expected output:

******
*    *
*    *
*    *
******

C Program

#include <stdio.h>

int main()
{
    int i, j;

    for (i = 1; i <= 5; i++)
    {
        for (j = 1; j <= 6; j++)
        {
            if (i == 1 || i == 5 || j == 1 || j == 6)
            {
                printf("*");
            }
            else
            {
                printf(" ");
            }
        }

        printf("\n");
    }

    return 0;
}

Sample Output

******
*    *
*    *
*    *
******

Explanation

A star is printed when the position is on the border.

The condition:

i == 1 || i == 5 || j == 1 || j == 6

means:

  • First row → print *
  • Last row → print *
  • First column → print *
  • Last column → print *

Otherwise, a space is printed.

Concepts Covered

  • Nested loops
  • if-else
  • Logical OR ||
  • Rows and columns
  • Hollow pattern

Q8. Print a Right-Aligned Star Triangle

Problem Statement

Write a C program to print:

    *
   **
  ***
 ****
*****

C Program

#include <stdio.h>

int main()
{
    int i, j;

    for (i = 1; i <= 5; i++)
    {
        for (j = 1; j <= 5 - i; j++)
        {
            printf(" ");
        }

        for (j = 1; j <= i; j++)
        {
            printf("*");
        }

        printf("\n");
    }

    return 0;
}

Sample Output

    *
   **
  ***
 ****
*****

Explanation

This pattern uses two inner loops.

The first inner loop prints spaces:

for (j = 1; j <= 5 - i; j++)

The second inner loop prints stars:

for (j = 1; j <= i; j++)

For the first row:

4 spaces + 1 star

For the second row:

3 spaces + 2 stars

and so on.

Concepts Covered

  • Nested loops
  • Multiple inner loops
  • Spaces
  • Right alignment
  • Pattern design

Q9. Print a Number Pyramid

Problem Statement

Write a C program to print the following pattern:

    1
   123
  12345
 1234567
123456789

C Program

#include <stdio.h>

int main()
{
    int i, j;

    for (i = 1; i <= 5; i++)
    {
        for (j = 1; j <= 5 - i; j++)
        {
            printf(" ");
        }

        for (j = 1; j <= 2 * i - 1; j++)
        {
            printf("%d", j);
        }

        printf("\n");
    }

    return 0;
}

Sample Output

    1
   123
  12345
 1234567
123456789

Explanation

There are two important calculations.

Spaces

The number of spaces is:

5 - i

Numbers

The number of numbers is:

2 × i - 1

For example, when i = 3:

Spaces = 5 - 3 = 2
Numbers = 2 × 3 - 1 = 5

So the row becomes:

  12345

Concepts Covered

  • Nested loops
  • Multiple inner loops
  • Mathematical expressions
  • Spaces
  • Pyramid pattern

Q10. Print a Multiplication Table Grid

Problem Statement

Write a C program to print multiplication tables from 1 to 5 in a grid.

C Program

#include <stdio.h>

int main()
{
    int i, j;

    for (i = 1; i <= 5; i++)
    {
        for (j = 1; j <= 10; j++)
        {
            printf("%d x %d = %d\n", i, j, i * j);
        }

        printf("\n");
    }

    return 0;
}

Sample Output

1 x 1 = 1
1 x 2 = 2
1 x 3 = 3
...
1 x 10 = 10

2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
...
2 x 10 = 20

3 x 1 = 3
...

Explanation

The outer loop controls which table is being printed:

for (i = 1; i <= 5; i++)

The inner loop controls the multiplier:

for (j = 1; j <= 10; j++)

For example:

i = 3
j = 4

produces:

3 x 4 = 12

This same row-column concept is useful later when working with two-dimensional arrays and matrices.

Concepts Covered

  • Nested loops
  • Multiplication
  • Row-column logic
  • Practical nested-loop application

Key Takeaways

  • A nested loop is a loop inside another loop.
  • The outer loop commonly controls rows.
  • The inner loop commonly controls columns or repeated work.
  • Pattern problems are a good way to practice nested-loop logic.
  • i and j are commonly used as outer and inner loop variables.
  • Spaces can be printed using an additional inner loop.
  • if-else can be combined with nested loops to create hollow patterns.
  • Nested loops are useful beyond patterns, including tables, matrices, arrays, and grids.
  • Always update the loop variables correctly to avoid infinite loops.
  • printf("\n") is commonly used to move to the next row.
  • Understanding rows and columns makes two-dimensional array problems easier later.

FAQs

1. What is a nested loop in C?

A nested loop is a loop placed inside another loop.

Example:

for (i = 1; i <= 5; i++)
{
    for (j = 1; j <= 5; j++)
    {
        printf("*");
    }
}

2. Which loop is the outer loop?

The loop written outside another loop is called the outer loop.

In pattern programs, it commonly controls the number of rows.

3. Which loop is the inner loop?

The loop placed inside the outer loop is the inner loop.

It commonly controls the number of columns, spaces, stars, or numbers printed in each row.

4. Why are nested loops used for pattern programs?

Patterns normally contain multiple rows and columns. The outer loop can control the rows while the inner loop controls what is printed inside each row.

5. Can we use while loops for pattern problems?

Yes. Pattern problems can be solved using for, while, or do-while loops.

For beginners, for loops are often convenient because initialization, condition, and update are visible together.

6. How do I create a right-aligned pattern?

Usually, print spaces before printing the required characters.

For example:

for (j = 1; j <= 5 - i; j++)
{
    printf(" ");
}

for (j = 1; j <= i; j++)
{
    printf("*");
}

The first loop creates the indentation and the second loop creates the stars.

7. How many times does an inner loop execute?

It depends on the condition of the inner loop.

For example:

for (i = 1; i <= 5; i++)
{
    for (j = 1; j <= i; j++)
    {
        printf("*");
    }
}

The inner loop executes:

1 + 2 + 3 + 4 + 5 = 15

times in total.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top