Home / Programs / C program to print the number pattern. 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
Programming Example

C program to print the number pattern.
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

👁 3,033 Views
💻 Practical Program
📘 Step by Step Learning
Pattern Programming

Program Code

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

Output

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Press any key to continue . . .

Explanation

In this program, nested while loop is used to print the pattern. The outermost loop runs 5 times and for every loop, the innermost loop runs i times which is 1 at first, meaning only "1" is printed, then on the next loop it's 2 numbers printing "1 2" and so on till 5 iterations of the loop executes, printing "1 2 3 4 5". This way, the given number pattern is printed.

How to learn from this program

First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.