Pattern Printing Using Nested For Loop


Explanation

Nested for loops are used to print different types of patterns. A nested loop means one for loop is placed inside another for loop. The outer loop controls the number of rows, while the inner loop controls what is printed in each row. By changing the starting value, ending value, or the condition of the inner loop, we can create a variety of number patterns.

How It Works?

  • Step 1: Declare integer variables r and c.
  • Step 2: Use the outer for loop to control the number of rows.
  • Step 3: Use the inner for loop to control the numbers printed in each row.
  • Step 4: Print the required value inside the inner loop according to the pattern.
  • Step 5: After the inner loop completes, print a new line using printf("\n");.
  • Step 6: Repeat the process until all rows of the pattern are displayed.

Example Program 1


#include <stdio.h>
#include <conio.h>

main()
{

    int r, c;

    clrscr();

    for(r = 1; r <= 5; r++)
    {

        for(c = 1; c <= 5; c++)
        {

            printf("%d ", c);

        }

        printf("\n");

    }

    getch();

}

Output

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

Example Program 2


#include <stdio.h>
#include <conio.h>

main()
{

    int r, c;

    clrscr();

    for(r = 1; r <= 5; r = r + 1)
    {

        for(c = 1; c <= r; c = c + 1)
        {

            printf("%d ", c);

        }

        printf("\n");

    }

    getch();

}

Output

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

Example Program 3


#include <stdio.h>
#include <conio.h>

main()
{

    int r, c;

    clrscr();

    for(r = 1; r <= 5; r++)
    {

        for(c = 5; c >= r; c--)
        {

            printf("%d ", c);

        }

        printf("\n");

    }

    getch();

}

Output

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

Example Program 4


#include <stdio.h>
#include <conio.h>

main()
{

    int r, c;

    clrscr();

    for(r = 5; r >= 1; r--)
    {

        for(c = 1; c <= r; c++)
        {

            printf("%d ", r);

        }

        printf("\n");

    }

    getch();

}

Output

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

Practice Exercises

Task 1: Write a C program using nested for loops to print the following pattern.

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

Goal: Learn how nested for loops can be used to print a pattern by controlling rows and repeating the row number using the inner loop.

💡 Hint: Use two variables r and c. Run the outer loop from 1 to 5. Run the inner loop from 1 to r and print the value of r inside the inner loop.
💡 Show Solution


#include <stdio.h>
#include <conio.h>


main()
{


    int r, c;


    clrscr();


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

    {


        for(c = 1; c <= r; c++)

        {


            printf("%d ", r);


        }


        printf("\n");


    }


    getch();


}


Output
1 2 2 3 3 3 4 4 4 4 5 5 5 5 5
Explanation: The program uses nested for loops to create a number pattern. The outer loop controls the rows, while the inner loop repeats the current row number according to the row value. After completing each row, a new line is printed to display the next pattern row.